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 879749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:59:00+00:00 2026-05-15T11:59:00+00:00

I’m rather new to Ruby, and so far, figuring out how to use binding

  • 0

I’m rather new to Ruby, and so far, figuring out how to use “binding” objects is one of the biggest pain points for me. If I’m reading the documentation correctly, they’re almost entirely opaque. To access the scope inside the binding object, you have to have a string of Ruby code and eval it using the binding.

Maybe I’m just a purist from a different school, but I’m allergic to string-based ‘eval’ constructs, generally speaking. Is there any way to do any of the following, securely and in the general case, given a binding object:

  1. List the identifiers in scope in the context the binding represents, or retrieve a hash of the contents.
  2. Set the value of a local variable in the binding equal to that of some local variable in an external context. Ideally, this should work generally, even if the value is an object reference, file handle, or some other complex entity.
  3. (extension 2:) Given a hash, set locals in the binding for each entry.
  4. Better yet, given a hash build a binding with only the basic language constructs and the names in the hash in scope.

Basically, I want to know which of those is possible and how to accomplish the ones that are. I imagine that the solutions for each will be fairly closely related, which is why I’m putting all of this in a single question.

Alternatively, is there any way to eval code that’s already been parsed in the context of a binding, similar to Perl’s eval BLOCK syntax?

  • 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-15T11:59:01+00:00Added an answer on May 15, 2026 at 11:59 am

    On searching more, I found an answer to at least part of my question:

    Based on: http://wikis.onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print

    The rest is from experimentation after Jim Shubert’s helpful pointers.

    1. This can be accomplished by eval-ing local_variables, instance_variables and global_variables inside the binding.
    2. You can do something as described below, given var_name, new_val, my_binding (syntax may be imperfect or improvable, feel free to suggest in comments. Also, I couldn’t get the code formatting to work inside the list, suggestions for how to do that will also be implemented.)
    3. You can straightforwardly take (2) and loop the hash to do this.
    4. See the second code block below. The idea is to start with TOPLEVEL_BINDING, which I believe normally just includes the global variables.

    This does involve using string eval. However, no variable values are ever expanded into the strings involved, so it should be fairly safe if used as described, and should work to ‘pass in’ complex variable values.

    Also note that it’s always possible to do eval var_name, my_binding to get a variable’s value. Note that in all of these uses it’s vital that the variable’s name be safe to eval, so it should ideally not come from any kind of user input at all.

    Setting a variable inside a binding given var_name, new_val, my_binding:

    # the assignment to nil in the eval coerces the variable into existence at the outer scope
    setter = eval "#{var_name} = nil; lambda { |v| #{var_name} = v }", my_binding
    setter.call(new_val)
    

    Building a “bespoke” binding:

    my_binding = eval "lambda { binding }", TOPLEVEL_BINDING # build a nearly-empty binding
    # set_in_binding is based on the above snippet
    vars_to_include.each { |var_name, new_val| set_in_binding(var_name, new_val, my_binding) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer With over one million active users, the JW Player™ is… May 15, 2026 at 1:29 pm
  • Editorial Team
    Editorial Team added an answer Create a custom class public class PageBase:Page { protected override… May 15, 2026 at 1:29 pm
  • Editorial Team
    Editorial Team added an answer After struggling with this problem for awhile longer I learned… May 15, 2026 at 1:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.