Created
July 15, 2011 12:18
-
-
Save andruby/1084588 to your computer and use it in GitHub Desktop.
method introspection in Ruby 1.9.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include Test::Unit::Assertions | |
#=> Object | |
m = method(:assert_in_delta) | |
#=> #<Method: Object(MiniTest::Assertions)#assert_in_delta> | |
m.methods - Object.methods | |
#=> [:call, :[], :arity, :to_proc, :receiver, :owner, :unbind, :source_location, :parameters] | |
m.parameters | |
#=> [[:req, :exp], [:req, :act], [:opt, :delta], [:opt, :msg]] | |
m.arity | |
#=> -3 | |
m.source_location | |
#=> ["/Users/andrew/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/minitest/unit.rb", 122] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
def initialize(fuel_type, passenger_capacity, door = 3) | |
puts "TODO" | |
end | |
end | |
Car.instance_method(:initialize).parameters | |
#=> [[:req, :fuel_type], [:req, :passenger_capacity], [:opt, :door]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Method introspection works on all methods in Ruby. Also methods defined in Kernel (without source_location).
This is a great way to dynamically inspect the names of the arguments passed to any method.