Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Methods defined with #define_method have no parameters #2204

Closed
ryanong opened this Issue Mar 14, 2013 · 2 comments

Comments

Projects
None yet
3 participants

ryanong commented Mar 14, 2013

class Foo
  define_method(:bar) { |arg| }
end

Foo.instance_method(:bar).parameters #=> [[:req, :arg]]
Foo.new.method(:bar).parameters #=> []

Kind of fascinating really.
I am making a new mocking library and I use some gnarly hacks using parameters inspection to detect if the method is stubbed.
http://github.com/ryanong/spy

Kernel#method uses Rubinius.find_method(self, name)
https://github.com/rubinius/rubinius/blob/master/kernel/common/kernel19.rb#L4

And for dynamic methods it returns a Rubinius::BlockEnvironment::AsMethod

AsMethod#parameters is not defined so when it is sent into Method it just returns []
https://github.com/rubinius/rubinius/blob/master/kernel/bootstrap/block_environment.rb#L25
https://github.com/rubinius/rubinius/blob/master/kernel/common/method.rb#L106

However when you use Module#instance_method it sends the AsMethod object into an UnboundMethod which converts #local_names into the correct #parameters format.

This conversion code for local_names is nearly the same as what is found in Proc. In MRI blocks for the most part are almost always a Proc except when a block is sent to #define_method or #define_singleton_method where it is turned into a Lambda.

@ghost ghost assigned YorickPeterse Mar 27, 2013

Owner

dbussink commented Mar 31, 2013

Looks like this is already fixed with the work of @YorickPeterse. We should probably make sure that this behavior is covered with specs though, don't know if they have been added for this cases when Method#parameters was fixed.

Owner

dbussink commented Apr 12, 2013

I'm closing this one, since this bug has been fixed by refactoring Method#parameters

@dbussink dbussink closed this Apr 12, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment