In general, Ruby's reflection capabilities are pretty powerful, although not always logical. However, when reflecting on a method's (or proc's) usage, you are sometimes stuck with sad methods.
Sad methods only work for code, that is written in Ruby itself. And Ruby itself (official MRI) is written in C, which limits such methods' usefulness quite a lot. This is an implementation specific problem, and it naturally does not occur in implementations like Rubinius. The following three methods are affected:
Arity
The arity of a method (or proc) will return:
- A positive number if the method has a fixed number of arguments
- A negative number if the method has a variable number of arguments, the value denoting the number of required arguments
For example:
method(:require).arity # => 1
However: For methods written in C, returns -1 if the call takes a variable number of arguments
method(:puts).arity # => -1
Source Location
The source_location
of a method (or proc) returns a two-element array containing the path to the source file and the line number, where it is defined:
method(:require).source_location
# => ["/home/jan/.rvm/rubies/ruby-2.3.0/lib/.../core_ext/kernel_require.rb", 39]
However: Returns […] nil if this method was not defined in Ruby (i.e. native)
method(:puts).source_location # => nil
Parameters
The Method#parameters
method lets inspect what kind of parameters a method (or proc) takes:
FileUtils.method(:cd).parameters
# => [[:req, :dir], [:opt, :options], [:block, :block]]
However: Like arity
, it will treat all C methods with multiple arguments as "variable number of arguments", instead of providing exact information:
Module.instance_method(:define_method).parameters
#=> [[:rest]]
Also See
- method_source: Retrieve the method source via source_location
- howtocall: Extended version of
Method#parameters
More Idiosyncratic Ruby
- Please Comment on GitHub
- Next Article: Fixed Numbers
- Previous Article: Static Monkeys vs. Strong Ducks