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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:29:43+00:00 2026-06-10T06:29:43+00:00

I have the following code: code = def hi; \hi!\; end eval code hi

  • 0

I have the following code:

code = "def hi; \"hi!\"; end"
eval code

hi == "hi!" # true

I can access the method hi, because when it is defined in the evaluated code, it is defined as a method of the main object.

However, this also means that the evaluated code can access things I define outside of it:

def hi; "hi!"; end
eval "hi == \"hi\"" # => true

I want to have a separate namespace in which I can run evaluated code; How would I do it?

I tried playing with module_eval and using a module for the namespace, but I can’t get it to define a method or a class and access that in another evaluation.

  • 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-10T06:29:45+00:00Added an answer on June 10, 2026 at 6:29 am

    You can evaluate code inside of an object like this:

      module DSL
        def helper_method_to_be_used_by_evaled_code
          # ...
        end
    
        # ...
      end
    
      container = Object.new
      container.extend(DSL)
      File.open(eval_file1, 'r') {|f| container.instance_eval(f.read, eval_file1)}
      File.open(eval_file2, 'r') {|f| container.instance_eval(f.read, eval_file2)}
    

    With this you can control whether to preserve definitions between evals or not: you can reuse container instance or dispose it and create new one. This would work with method definitions and constants, local variables aren’t preserved between such calls.

    Also, instead of Object you may want to look at BlankState pattern. “blankslate” is an example.

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

Sidebar

Related Questions

Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I have the following code: class MyClass def method foo = MyClass.all end end
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
I have the following code: def show @game = $site.new_game_of_type(params[:id]) @game.start end def update_game_time
I have following code: module CarHelper def call_helpline puts Calling helpline... end end class
I have the following code: def bootstrap_form_field(f, method, text, &block) content_tag :div, :class =>
I have the following Code: def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new fields =
I have the following code: def incoming_acceptation(incoming_code) if invite_code == incoming_code accepted = true
I have the following piece of code def show unless logged_in? login_required return end

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.