overriding Object::send

J

Joe Van Dyk

Why doesn't this work?

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

ara.t.howard

Why doesn't this work?

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

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
 
J

Jacob Fugal

Try:

class Object
alias_method :eek:ld_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
=3D> 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 Fugal
 
J

Joe Van Dyk

Try:

class Object
alias_method :eek:ld_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
=3D> 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. :)

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)
 
R

Ross Bamford

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?

Maybe this would do it (with the caveat noted):

class Binding
def locals
eval('local_variables',self).inject({}) do |hsh, var|
hsh[var.intern] = eval(var, self)
hsh
end
end
end

def foo(one, two = 'default')
'foo!'
end

def bar(*args)
'bar!'
end

def baz(arg = 7, &blk)
'baz!'
end

set_trace_func proc { |event, file, line, id, binding, classname|
if event == 'call'
puts "Called #{classname}.#{id} with args #{binding.locals.inspect}"
end
}

foo('arg')
foo('arg_one', 'arg_two')
bar
bar('these','are','my','args')
baz(4)

# NOTE: failure here, doesn't see the block
baz { |b| puts b }
baz(11, &proc { |b| puts b })


Output:

Called Object.foo with args {:eek:ne=>"arg", :two=>"default"}
Called Object.foo with args {:eek:ne=>"arg_one", :two=>"arg_two"}
Called Object.bar with args {:args=>[]}
Called Object.bar with args {:args=>["these", "are", "my", "args"]}
Called Object.baz with args {:arg=>4, :blk=>nil}
Called Object.baz with args {:arg=>7, :blk=>nil}
Called Object.baz with args {:arg=>11, :blk=>nil}

Also, the extensions/binding
(http://extensions.rubyforge.org/rdoc/index.html) adds and changes the
binding, so you can just do:

bin = <a binding>
lvs = bin.local_variables.inject({}) { |hsh, v| hsh[v.intern] = bin[v];
hsh }

instead of modifying Binding yourself if you like, but I'm a bit confused
about the status of facets, extensions, and so on - not sure which are
current and which aren't :(

Cheers,
 

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,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top