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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:22:09+00:00 2026-05-28T15:22:09+00:00

I want to run eval of strings to define both local variables and constants.

  • 0

I want to run eval of strings to define both local variables and constants.
I want to do this in different namespaces. I can do this with local variables
but not with constants. Is there a way to modify the NameSpaces module below
so that constants defined in one binding/namespace are not seen by another?

# Example run under ruby 1.9.1
module NameSpaces
  def self.namespace(namespace)
    return binding
  end
end

b1 = NameSpaces.namespace("b1")
b2 = NameSpaces.namespace("b2")

# Set a 'x', then check to make sure its still set in the scope of 'b1'
puts b1.eval("x = 1") # => 1
puts b1.eval("x")     # => 1

# Check to make sure 'x' is NOT set in the scope of 'b2'
begin
  puts b2.eval("x") # NameError exception expected here
rescue Exception => e
  puts e.to_s       # => undefined local variable or method `x'
                    #  for NameSpaces:Module (THIS IS AS EXPECTED.)
end

# Set constant 'C' and do the same checks
puts b1.eval("C = 1") # => 1
puts b1.eval("C")     # => 1

# Check to make sure 'C' is NOT set in the scope of 'b2'
begin
  puts b2.eval("C")  # (I DON'T GET AN EXCEPTION.  NOT AS I HAD HOPED FOR.)
rescue Exception => e
  puts e.to_s
end

Thanks so much for the look. I’m very stuck.

  • 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-28T15:22:10+00:00Added an answer on May 28, 2026 at 3:22 pm

    The behavior you are observing is normal. When you execute C = 1 in the scope of a call to NameSpaces.namespace, a constant “C” is defined on NameSpaces. (You can confirm this by trying NameSpaces::C.)

    To get the effect you want, you need to use the binding of an anonymous module. Try this:

    namespace1 = Module.new.class_eval("binding")
    namespace2 = Module.new.class_eval("binding")
    namespace1.eval("C = 1")
    namespace1.eval("C")
    => 1
    namespace2.eval("C")
    NameError: uninitialized constant #<Module:0xf09180>::C
    

    Note that any constants which are defined in Object (i.e. the global scope) will be available within the code passed to eval, and if the values of such constants are changed in the evaluated code, the change will be visible globally!

    (Even if you evaluate code in the context of a BasicObject, which doesn’t inherit from Object, the evaluated code can still access constants defined on Object, by prefixing the name with ‘::’)

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

Sidebar

Related Questions

When using mongo shell I can run something like this: db.sandbox.insert({line : db.eval('storedFunction()') })
Why this code don't work,when i want run this code vwd 2008 express show
I have written php program and uploaded on server. I want run this program
I want to run this on my table: ALTER TABLE table_name MODIFY col_name VARCHAR(255)
This should be really easy but I can't figure out how to make it
This is a homework question, but as you can see I've written most of
Due to emacs interlocking files I want to run org-mobile command via cron but
I want run all script from the directory . Like , The directory contains
I want run a script as follows: runner: ssh 'java program &' ssh 'java
Want to run javascript function from parent window in child window Example I have

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.