Friday, March 11, 2011

Ruby Light #4

eval_string.rb
class EvalString < String
def eval(b)
b.eval String.new(self)
end
end


irb session
>> foo = 1
=> 1
>> bar = 2
=> 2
>> str = EvalString.new "foo+bar"
=> "foo+bar"
>> str
=> "foo+bar"
>> str.eval(binding)
=> 3

No comments: