In Ruby 1.8.7 I can do the following in order to get the subject of a binding object:
binding.eval("self")
However, in Ruby 1.8.6, the eval method is private, so I expose it like this:
class Binding
public :eval
end
Which seems to work fine, however, binding.eval("self") returns the binding itself, not the subject of binding.
How can I get the subject of a binding in Ruby 1.8.6? The solution doesn’t need to pretty – it just needs to work until we can upgrade to 1.8.7.
I’ll bet at least a nickel
eval('self', abinding)will work:This works because if you pass a binding to eval, it evaluates the string in the context of that binding.
selfin the context of the binding is whateverselfwas when the binding was created.