In Ruby 1.9.2 you can inspect the parameters of any method using method(:symbol). How could I inspect the Car#initialize method without having to create a new car?
class Car
def initialize(fuel_type, passenger_capacity, door = 3)
puts "TODO"
end
end
Car.new(nil,nil).method(:initialize).parameters
#=> [[:req, :fuel_type], [:req, :passenger_capacity], [:opt, :door]]
Instance_eval didn’t work:
Car.instance_eval { |x| p x.method(:initialize).parameters } #=> [[:rest]]
1 Answer