[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Robert Dober

2/13/2008 3:38:00 PM

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.




















--
http://ruby-smalltalk.blo...

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

9 Answers

John Joyce

2/13/2008 4:47:00 PM

0


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.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> http://ruby-smalltalk.blo...
>
> ---
> 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

Morton Goldberg

2/13/2008 5:14:00 PM

0

On Feb 13, 2008, at 10:38 AM, 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.
>
> <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

Robert Dober

2/13/2008 5:58:00 PM

0

On Feb 13, 2008 6:13 PM, Morton Goldberg <m_goldberg@ameritech.net> wrote:
> On Feb 13, 2008, at 10:38 AM, 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.
> >
> > <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
>
>

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

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

Morton Goldberg

2/13/2008 6:13:00 PM

0

On Feb 13, 2008, at 12:58 PM, Robert Dober 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

Martin DeMello

2/13/2008 6:31:00 PM

0

On Feb 13, 2008 9:08 PM, Robert Dober <robert.dober@gmail.com> 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.
>
> <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

Robert Dober

2/13/2008 8:31:00 PM

0

On Feb 13, 2008 7:31 PM, Martin DeMello <martindemello@gmail.com> wrote:
> On Feb 13, 2008 9:08 PM, Robert Dober <robert.dober@gmail.com> 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.
> >
> > <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
>
>
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.blo...

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

Morton Goldberg

2/13/2008 9:51:00 PM

0

On Feb 13, 2008, at 3:30 PM, Robert Dober 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

James Gray

2/13/2008 10:11:00 PM

0

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

Robert Dober

2/14/2008 1:04:00 PM

0

On Wed, Feb 13, 2008 at 11:11 PM, James Gray <james@grayproductions.net> 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.
> >
> > <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.

--
http://ruby-smalltalk.blo...

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