How does Object#send work?

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.

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.

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.

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

···

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.

--
Brian Schröder
http://ruby.brian-schroeder.de/