Redefining send()

I tried to write a small piece of code as a wrapper around send(), but for
some reason it appears not to be called.

I did a search in the Ruby-Talk archive for this, but the only two entries I
found seemed to imply that you can’t do it.

If this has been discussed in more detail, can someone point me at some
specific entries?

I’m guessing that send() is considered a special case and ignored, so that
some kind of infinite recursion doesn’t happen.

In any case, I’d be interested to know why it doesn’t work. Can someone shed
some light on this?

My code looked like …

class Fred
alias old_send send

   def send(name, *args)
       $stderr.puts "send('#{name}')"
       old_send(name, *args)
   end

   # ...

end

Hence, I was expecting that when any method of Fred was called, I’d see the
message, and then the real code would be called. As I say, I never see the
message, so I assume the re-definition has been ignored … or maybe I’m
missing the point entirely!

Hi –

My code looked like …

class Fred
alias old_send send

   def send(name, *args)
       $stderr.puts "send('#{name}')"
       old_send(name, *args)
   end

   # ...

end

Hence, I was expecting that when any method of Fred was called, I’d see the
message, and then the real code would be called. As I say, I never see the
message, so I assume the re-definition has been ignored … or maybe I’m
missing the point entirely!

I can’t get it not to work:

class Fred
alias old_send send

def send(name, *args)
  $stderr.puts "send('#{name}')"
  old_send(name, *args)
end

def say(x)
  puts "hi: #{x}"
end

end

Fred.new.send(:say, “there”)

Output:

send(‘say’)
hi: there

(Ruby 1.6.7)

David

···

On Thu, 8 Aug 2002, Harry Ohlsen wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I can’t get it not to work:

Fred.new.send(:say, “there”)

Output:

send(‘say’)
hi: there

Sorry. For some reason, I was under the impression that all calls to methods
went through send() … who’s a silly boy, then :slight_smile: ?!

So, I was expecting that when I did “fred.hello()” it would end up calling
“fred.send(:hello)”. Of course, this is an incredibly silly idea !!

Thanks for setting me straight so quickly … you’ve probably saved me an
entire afternoon.