How does Object#send work?

  • Thread starter Giulio Piancastelli
  • Start date
G

Giulio Piancastelli

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

Yukihiro Matsumoto

Hi,

In message "Re: How does Object#send work?"

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

Giulio Piancastelli

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

Brian Schröder

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
 

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

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top