[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How does Object#send work?

Giulio Piancastelli

11/27/2004 4:20:00 PM

I'm wondering... does Object#send start from the Object class to search
for the method to execute? I'm leading to this conclusion because of a
little class I'm working on where I'm using send to execute methods,
and one of the methods I'm trying to execute is called 'select' and
takes a number as its parameter. Unfortunately, this name happens to
clash with Kernel.select, which instead takes an array as a parameter.
Thus, an exception is thrown, leading to the "wrong argument type
Fixnum (expected Array)" message.

I suppose it's impossible to make Object#send search from where the
method has been called instead of Object itself... but, is there a way
to have the B#select method called instead of the select method in
Kernel? I'd like not to change names nor the design of A and B, if
that's possible.

On the other hand, the following sketch just works:

class A
def make_it_so arg0
send(arg0.text, 1)
end
end

class B < A
def select index
puts index
end
end

b = B.new
b.make_it_so(param) # where param.text returns 'select'

so, it's probably that I don't understand what's really happening...
Can anyone help enlight me on the topic?
Best Regards,
Giulio Piancastelli.

3 Answers

Yukihiro Matsumoto

11/27/2004 4:28:00 PM

0

Hi,

In message "Re: How does Object#send work?"
on Sun, 28 Nov 2004 01:22:53 +0900, "Giulio Piancastelli" <giulio.piancastelli@gmail.com> writes:

|I'm wondering... does Object#send start from the Object class to search
|for the method to execute?

"send" invokes the method of the receiver, i.e. you tried to invoke
"select" method of class A object. You should have done

arg0.send(arg0.text, 1)

instead.

matz.


Giulio Piancastelli

11/27/2004 4:30:00 PM

0

Nevermind, sorry to have cluttered the group. I was actually calling
'send' on a NilClass instance, and that's resolved in calling the only
'select' method known, that is Kernel.select. Now I've solved, the
problem, please excuse me again.

Regards,
Giulio Piancastelli.

Brian Schröder

11/27/2004 4:37:00 PM

0

On Sun, 28 Nov 2004 01:22:53 +0900
"Giulio Piancastelli" <giulio.piancastelli@gmail.com> wrote:

> I'm wondering... does Object#send start from the Object class to search
> for the method to execute? I'm leading to this conclusion because of a
> little class I'm working on where I'm using send to execute methods,
> and one of the methods I'm trying to execute is called 'select' and
> takes a number as its parameter. Unfortunately, this name happens to
> clash with Kernel.select, which instead takes an array as a parameter.
> Thus, an exception is thrown, leading to the "wrong argument type
> Fixnum (expected Array)" message.
>
> I suppose it's impossible to make Object#send search from where the
> method has been called instead of Object itself... but, is there a way
> to have the B#select method called instead of the select method in
> Kernel? I'd like not to change names nor the design of A and B, if
> that's possible.
>
> On the other hand, the following sketch just works:
>
> class A
> def make_it_so arg0
> send(arg0.text, 1)
> end
> end
>
> class B < A
> def select index
> puts index
> end
> end
>
> b = B.new
> b.make_it_so(param) # where param.text returns 'select'
>
> so, it's probably that I don't understand what's really happening...
> Can anyone help enlight me on the topic?
> Best Regards,
> Giulio Piancastelli.
>
>

It seems to work here


bschroed@black $ cat send.rb
class A
def send_it method_name
send(method_name, "Send from #{self}")
end
end

class B < A
def select(msg)
puts "Received: #{msg}"
end
end

b = B.new
b.send_it(:select)
bschroed@black $ ruby send.rb
Received: Send from #<B:0x402a9b9c>
bschroed@black $ ruby -v
ruby 1.8.2 (2004-11-23) [i386-linux]


regards,

Brian

--
Brian Schröder
http://ruby.brian-sch...