From: Robert Dober Date: 2008-02-14T22:03:39+09:00 Subject: Re: [RRR] Robert's Ruby Riddle: Local or Method On Wed, Feb 13, 2008 at 11:11 PM, James Gray wrote: > 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. > > > > > > a = 42 > > def a; 42 end > > puts a > > > > 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 > > I thought the debugger and the profiler were the only solutions, that is quite one James! R. -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein