nPn nPn - 1 year ago 59
Ruby Question

In Ruby is there a way to define the context for load?

I would like to load a file in a block and evaluate the statements in the context of an instance of a object. For example something like this:

I have a file called foo_file that contains just this one line (statement)


I would like this "statement" to be called in the context of an instance of Foo, as shown below.

class Foo
def some_method
puts "hello"

foo =

foo.instance_eval do
load "foo_file" # load and execute statement some_method from a foo_file.

This fails with:

foo_file:1:in `<top (required)>': undefined local variable or method `some_method' for main:Object (NameError)

while this works fine:

foo.instance_eval do

So it seems that while I call load in the block passed to instance_eval, the statements are loaded into the top context, not the instance foo.

I can get around this with the code shown below. I create a top level method that wraps a call to the instance method. I still need to provide the context, which I accomplish with a singleton.

require 'singleton'

class Context
include Singleton
attr_accessor :ctx

def some_method
ctx = Context.instance.ctx

Context.instance.ctx = foo
load "bar_file"

I think this will work, but it seems like there should be a better way (like just loading into the correct context to begin with). Any suggestions?

Answer Source

UPDATE: Actually, there is an even simpler way to do this:


For example, if file just had the text length, then


would return 5.


This is exactly the sort of situation you'd want to use Kernel#eval with the optional Binding argument in:

foo.instance_eval do
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download