What are [[:rest]] or [[:req]] in ruby?

I am very new to this method :
Method#parameters(Class: Method (Ruby 2.0.0))

12.method(“+”).parameters

=> [[:req]]

[].method(:inject).parameters

=> [[:rest]]

What does the output mean by [[:rest]] or [[:req]] ?

Please help…

def m(a,b=3,*c,d: 4,e:, **g,&f)
end

method(:m).parameters
#=> [[:req, :a], [:opt, :b], [:rest, :c], [:keyreq, :e], [:key, :d],
[:keyrest, :g], [:block, :f]]

puts method(:m).parameters.map(&:first)

#=>
req #required argument
opt #optional argument
rest #rest of arguments as array
keyreq #reguired key argument (2.1+)
key #key argument
keyrest #rest of key arguments as Hash
block #block parameter

remember: ruby can not know about the names of the parameters for
c-compiled fuctions

Hans M. wrote in post #1119372:

You are really great. I am very much thankful to you…You always helped
me to clear my doubts…although some times got angry on me like
others…But still I love this Ruby community members as they are really
helpful… :slight_smile: :slight_smile:

Help me to enjoy this awesome Ruby language… :slight_smile:

req #required argument
opt #optional argument
rest #rest of arguments as array
keyreq #reguired key argument (2.1+)
key #key argument
keyrest #rest of key arguments as Hash
block #block parameter

remember: ruby can not know about the names of the parameters for
c-compiled fuctions

On Aug 23, 2013, at 9:57 AM, Love U Ruby [email protected] wrote:

class Foo
def initialize(a, b); end
end
p Foo.method(:new).parameters

=> [[:rest]]

why I am not getting those name as a and b ?


Posted via http://www.ruby-forum.com/.

Foo.new is different method than Foo#initialize:

(from ruby core)

class.new(args, …) → obj


Calls allocate to create a new object of class’s class, then
invokes that object’s initialize method, passing it args. This
is the method that ends up getting called whenever an object is
constructed
using .new.

Since initialize is an instance method, you can see the parameter list
as:

Foo.instance_method(:initialize).parameters #=> [[:req, :a], [:req, :b]

class Foo
def initialize(a, b); end
end
p Foo.method(:new).parameters

=> [[:rest]]

why I am not getting those name as a and b ?

tamouse m. wrote in post #1119460:

On Aug 23, 2013, at 9:57 AM, Love U Ruby [email protected] wrote:

Ohh! My bad… thanks for the catch! :slight_smile:

(from ruby core)

class.new(args, …) → obj


Calls allocate to create a new object of class’s class, then
invokes that object’s initialize method, passing it args. This
is the method that ends up getting called whenever an object is
constructed
using .new.

Since initialize is an instance method, you can see the parameter list
as:

Foo.instance_method(:initialize).parameters #=> [[:req, :a], [:req, :b]