Tuesday, August 25, 2009

Method#parameters - Ruby 1.9.2 new feature

Most friendly new feature in Ruby 1.9.2 is Method#parameters. What is this? see the sample below:
> /opt/ruby-1.9.1/bin/irb
irb(main):001:0> def sample_method(a, b=0, *c, &d);end
=> nil
irb(main):002:0> self.method(:sample_method).parameters
=> [[:req, :a], [:opt, :b], [:rest, :c], [:block, :d]]

As you see, Method#parameters returns 2-dimensional array, not hash.

In each inner array, the first is parameter type and the last is parameter name. Parameter type is represented by symbol; :req is normal parameter, :opt is parameter with default value, :rest is variable parameter, and :block is block parameter.


Piyush said...

this is not there in ruby-1.9.2 ?

Youhei Kondou said...

No, still in there. I check to work above irb samples

Post a Comment