Forum: Ruby overriding Object::send

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe Van D. (Guest)
on 2006-01-17 02:09
(Received via mailing list)
Why doesn't this work?

class Object
  alias_method :old_send, :send
  def send meth, *args
    STDERR.puts "#{ meth } was called"
    old_send meth, args
  end
end
Caleb T. (Guest)
on 2006-01-17 02:09
(Received via mailing list)
On Monday 16 January 2006 15:11, Joe Van D. wrote:
> Why doesn't this work?

It works for me if I use "module Kernel" instead of "class Object".
unknown (Guest)
on 2006-01-17 03:16
(Received via mailing list)
On Tue, 17 Jan 2006, Joe Van D. wrote:

>
harp:~ > cat a.rb
   class Object
    def send meth, *a, &b
      STDERR.puts "#{ self.class }##{ meth }"
     __send__ meth, *a, &b
    end
   end

   42.send "display"


   harp:~ > ruby a.rb
   Fixnum#display
   42

hth.

-a
Jacob F. (Guest)
on 2006-01-17 03:16
(Received via mailing list)
Try:

class Object
   alias_method :old_send, :send
   def send meth, *args
     STDERR.puts "#{ meth } was called"
     old_send meth, *args
   end
end

The difference is the splat in front of args in the invocation of
old_send. With the splat there, it seems to work for me.

irb> -1.send(:abs)
abs was called
=> 1

This used to give me an error about wrong number of arguments.

Note: this still doesn't work without invoking send explicitly, since
foo.bar is *actually* equivalent to foo.__send__(:bar), *not*
foo.send(:bar). It just so happens the the default implementation of
send just forwards to __send__. And overwriting __send__ is a *bad*
idea. :)

Jacob F.
Joe Van D. (Guest)
on 2006-01-21 16:40
(Received via mailing list)
On 1/16/06, Jacob F. <removed_email_address@domain.invalid> wrote:
> The difference is the splat in front of args in the invocation of
> foo.send(:bar). It just so happens the the default implementation of
> send just forwards to __send__. And overwriting __send__ is a *bad*
> idea. :)

Hm.  Well, essentially what I want to do is output print statements
every time I enter and leave a method.  (having problems identifying
what's going on in my code)
Jacob F. (Guest)
on 2006-01-21 16:40
(Received via mailing list)
On 1/16/06, Joe Van D. <removed_email_address@domain.invalid> wrote:
> Hm.  Well, essentially what I want to do is output print statements
> every time I enter and leave a method.  (having problems identifying
> what's going on in my code)

Check out set_trace_func, may be what you need:

http://www.rubycentral.com/book/ref_m_kernel.html#...

Jacob F.
Joe Van D. (Guest)
on 2006-01-21 16:44
(Received via mailing list)
On 1/16/06, Jacob F. <removed_email_address@domain.invalid> wrote:
> On 1/16/06, Joe Van D. <removed_email_address@domain.invalid> wrote:
> > Hm.  Well, essentially what I want to do is output print statements
> > every time I enter and leave a method.  (having problems identifying
> > what's going on in my code)
>
> Check out set_trace_func, may be what you need:
>
> http://www.rubycentral.com/book/ref_m_kernel.html#...

Aha, I forgot about that one.

Say I want to display the arguments that each function gets.  Could I
get that from a binding object?
This topic is locked and can not be replied to.