From: Brian Candler Date: 2010-02-23T01:46:09+09:00 Subject: Re: Binding#eval issue Thomas Sawyer wrote: > I have the following extensions to Binding: > > class Binding > > # Returns line number of the binding. > def __LINE__ > eval("__LINE__") > end > > # Returns file name of the binding. > def __FILE__ > eval("__FILE__") > end > > end > > The work fine in 1.8.7 I tried this, and was surprised to find that 1.8.7 makes a special case of eval inside an instance method of Binding: $ irb --simple-prompt >> RUBY_VERSION => "1.8.7" >> RUBY_PATCHLEVEL => 174 >> class Binding; def __LINE__; eval "__LINE__"; end; end => nil >> class Binding; def __FILE__; eval "__FILE__"; end; end => nil >> binding.__LINE__ => 6 >> binding.__LINE__ => 7 >> binding.__FILE__ => "(irb)" >> binding.__FILE__ => "(irb)" This is particularly surprising given that instance_eval doesn't show this behaviour: >> binding.instance_eval "__LINE__" => 1 >> binding.instance_eval "__LINE__" => 1 >> binding.instance_eval "__FILE__" => "(eval)" >> binding.instance_eval "__FILE__" => "(eval)" __FILE__ is the filename where the source being executed is found. When eval'ing from a string, the source is not in any particular file and so I'd always expect "(eval)" to be returned, and __LINE__ to be relative to the start of the string, unless you pass file and/or line numbers as extra args to eval. In ruby 1.9.2 this works as I'd expect: $ irb19 --simple-prompt >> RUBY_REVISION => 24186 >> class Binding; def __LINE__; eval "__LINE__"; end; end => nil >> class Binding; def __FILE__; eval "__FILE__"; end; end => nil >> binding.__LINE__ => 1 >> binding.__LINE__ => 1 >> binding.__FILE__ => "(eval)" >> binding.__FILE__ => "(eval)" So I'd say that 1.8.7 is the anomoly. -- Posted via http://www.ruby-forum.com/.