Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:44:54+00:00 2026-06-10T17:44:54+00:00

For example, let’s say I have block = proc { |n| #{downcase} #{n} }

  • 0

For example, let’s say I have

block = proc { |n| "#{downcase} #{n}" }

Now I want to evaluate that block in the scope of a String, but pass that block a variable. I know how to do the first part:

"Foo".instance_eval(&block)

But how to also pass a variable to that block?

I tried

"Foo".instance_eval { block.call(3) }

But it didn’t work, it wasn’t in the scope of the String.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:44:56+00:00Added an answer on June 10, 2026 at 5:44 pm

    Use instance_exec instead:

    instance_exec(arg…) {|var…| block } → obj

    Executes the given block within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables. Arguments are passed as block parameters.

    So this will make it go:

    "Foo".instance_exec(3, &block)
    

    and give you the 'foo 3' that you desire.

    The problem with this:

    "Foo".instance_eval { block.call(3) }
    

    is that self will be "Foo" inside { block.call(3) } but not inside block, block will retain whatever self was when block was defined; there’s nothing in block.call(3) that forces a context so self doesn’t change. For example, given this:

    class C
      def m
        proc { |n| "#{downcase} #{n}" }
      end
    end
    c = C.new
    "Foo".instance_eval { c.m.call(3) }
    

    When the proc is called, self will be c because that’s what self was when the proc was defined (i.e. when m was called). What self is inside the instance_eval block doesn’t have any effect on self inside the proc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example let's say I have an app that uses $_SESSION['user_id'] . Now if
Maybe a silly question but here goes anyway. Example: Let's say I have one
For Example: Let's say that I want to return a view that displays a
For example let us say that we want to save a list of URLs
A simple example: Let's say I have one alias being sourced somewhere as: alias
Please indulge my simplistic example: Let's say I have 1 million pages on my
I want to capture all certain occurrences in a string in Vimscript. example: let
I'm struggling with the F# type signature notation. For example let's say you have
Let's say that I want to insert a record in a table. Each record
Really simple example: Let's say I have this table: ID Name GUID1 John GUID2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.