From: Robert Dober Date: 2008-02-14T05:30:57+09:00 Subject: Re: [RRR] Robert's Ruby Riddle: Local or Method On Feb 13, 2008 7:31 PM, Martin DeMello wrote: > On Feb 13, 2008 9:08 PM, Robert Dober wrote: > > > > 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. > > Here be spoiler: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > martin@dabba ~ $ cat puzzle.rb > a = 42 > def a; 42 end > puts a > > martin@dabba ~ $ ruby -r profile puzzle.rb 2>&1 | grep "Object#a" > > martin@dabba ~ $ tail -2 puzzle.rb > puzzle1.rb > > martin@dabba ~ $ cat puzzle1.rb > def a; 42 end > puts a > > martin@dabba ~ $ ruby -r profile puzzle1.rb 2>&1 | grep "Object#a" > 0.00 0.13 0.00 1 0.00 0.00 Object#a > > martin > > Bravo Martin, this is my preferred solution, another one would be the debugger, but that is not worth the effort. Too bad there are no prices, I just wanted to feature the profiler... Cheers Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein