Robert's Ruby Riddle: Local or Method

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.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Feb 13, 2008, at 9:38 AM, Robert D. wrote:

local variable “a” or the method “a”? And if so please explain how.

Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

very simple
puts a
will out put the object a
in the scope you def’d the ‘a’ method
it belongs to either Object or IRB
to get the method’s output you need to do
self.a

On Feb 13, 2008, at 10:38 AM, Robert D. wrote:

I can’t see how it can as it is written above, but with a little
modification …

a = 42 def a; 43 end puts a puts a()

This makes it clear that Ruby prefers the local variable when it’s
defined. It also makes it clear that one doesn’t have to write self.a
to get the method called.

# a = 42 def a; 43 end puts a puts a()

And this makes it clear that Ruby will call the method when the local
is not defined.

Regards, Morton

On Feb 13, 2008, at 12:58 PM, Robert D. wrote:

No Morton, there is a perfect solution with the code snippet I
posted… it is simple, I will tell you later

Really? Just by running the code as you posted it and looking at the
output? I’ll be impressed and surely learn something if that’s the
case. I look forward to your explanation.

Regards, Morton

On Feb 13, 2008 6:13 PM, Morton G. [email protected]
wrote:

I can’t see how it can as it is written above, but with a little
defined. It also makes it clear that one doesn’t have to write self.a
is not defined.

Regards, Morton

No Morton, there is a perfect solution with the code snippet I
posted… it is simple, I will tell you later
R.


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Feb 13, 2008 7:31 PM, Martin DeMello [email protected] wrote:

I leave some blank lines here so that people can reply without

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

On Feb 13, 2008, at 3:30 PM, Robert D. wrote:

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…

I admit using the profiler is clever, but a bit of an anticlimax. I
was hoping for something far more exciting and mysterious. However,
since I can Ruby right out of my text editor, I still think the code
variation scheme I proposed is the easier, more practical way to
decide the question.

Regards, Morton

On Feb 13, 2008 9:08 PM, Robert D. [email protected] wrote:

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

On Feb 13, 2008, at 9:38 AM, Robert D. wrote:

local variable “a” or the method “a”? And if so please explain how.

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 G. II

On Wed, Feb 13, 2008 at 11:11 PM, James G. [email protected]
wrote:

James Edward G. 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