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

  • SEARCH
  • Home
  • 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 7631273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:18:46+00:00 2026-05-31T06:18:46+00:00

I am trying to set a local variable to an existing binding def foo_callback

  • 0

I am trying to set a local variable to an existing binding

def foo_callback
  lambda { |name| p name }
end
b = foo_callback.binding

The binding doesn’t have any local variables to begin with:

b.eval("local_variables") # => []

Let us set a primitive local variable to the binding:

b.eval("age=30")

Everything works as expected:

b.eval("local_variables") # => ["age"]
b.eval("age") # => 30

Now, let us try to set a non-primitive local variable to the binding:

country = Country.first
b.eval("lambda {|v| country = v}").call(country)

Note: The technique for setting the variable is borrowed from the facet gem. I tried the ruby 1.9 safe implementation with same results.

The binding does not reflect the local variable country.

b.eval("local_variables") # => ["age"]

How do I get around this issue? Essentially, I want to declare a new variable in a binding using the value of an existing, non primitive variable.

I am on Ruby 1.8.7.

  • 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-05-31T06:18:48+00:00Added an answer on May 31, 2026 at 6:18 am

    You’re creating country outside of the binding, and then the country inside the lambda is only valid within that scope. Why not eval it if you need to inject that into the binding as well?

    Update

    Try declaring the variable outside of the scope of the lambda but inside the scope of the binding:

     b.eval("country = nil; lambda {|v| country = v}").call(country)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set a variable in my local.xml file for my custom block:
I am having a real headache trying to set a node's local position to
I've set up a project with an SDF local database file and am trying
Im trying to set the certificate friendly name during the certificate request/acceptance process. I
I am trying to create my own Perl module in /usr/local/lib/perl I have the
I'm trying to set a variable that should be accessible from outside PHP. Ideally
My understanding is that under ARC local variables should be cleared (set to 0).
I'm trying to set up a test that will tell me whether a variable
When you have a local variable named the same as a type, is there
I am trying to create persistante variable local to my model but although something

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.