[RRR] Robert's Ruby Riddle: Local or Method

R

Robert Dober

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.
 
J

John Joyce

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.
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
 
M

Morton Goldberg

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 can't see how it can as it is written above, but with a little
modification ...

<code>
a = 42
def a; 43 end
puts a
puts a()
</code>

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.

<code>
# a = 42
def a; 43 end
puts a
puts a()
</code>

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

Regards, Morton
 
R

Robert Dober

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

<code>
a = 42
def a; 43 end
puts a
puts a()
</code>

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.

<code>
# a = 42
def a; 43 end
puts a
puts a()
</code>

And this makes it clear that Ruby will call the method when the local
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.
 
M

Morton Goldberg

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
 
M

Martin DeMello

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.

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
 
R

Robert Dober

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
 
M

Morton Goldberg

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
 
J

James Gray

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
 
R

Robert Dober

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

I thought the debugger and the profiler were the only solutions, that
is quite one James!
R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top