James Gray
2/13/2008 10:11:00 PM
On Feb 13, 2008, at 9:38 AM, Robert Dober wrote:
> Hi list I was just thinking it might fun to present some of Ruby's
> features in form of a riddle.
> There was an interesting thread yesterday and as I always forget the
> basics I wrote some testcode to find out if the local or the method
> prevails, I made a stupid
> error and had to rewrite the code, or maybe did I not? This depends on
> the answer of the riddle.
> Todays Ruby Riddle is:
>
> Can the following code be used to test if the argument of puts is the
> local variable "a" or the method "a"? And if so please explain how.
>
> <code>
> a = 42
> def a; 42 end
> puts a
> </code>
> I leave some blank lines here so that people can reply without
> spoiling if for other readers.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
You can set a trace function for this:
$ ruby -e 'set_trace_func lambda { |event, _, _, name, _, _| puts
"method called" if event == "call" and name == :a }; eval(ARGF.read)'
riddle.rb
42
$ ruby -e 'set_trace_func lambda { |event, _, _, name, _, _| puts
"method called" if event == "call" and name == :a };
eval(ARGF.read.to_a[1..-1].join)' riddle.rb
method called
42
James Edward Gray II