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

**URL:** <https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297>\
**Category:** Ruby\
**Created:** [August 22, 2013, 8:20pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297 "2013-08-22T20:20:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 22, 2013, 8:20pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297/1 "2013-08-22T20:20:49Z")

</div>

I am very new to this method :  
Method#parameters([Class: Method (Ruby 2.0.0)](http://www.ruby-doc.org/core-2.0/Method.html#method-i-parameters))

12.method(“+”).parameters

# =\> [[:req]]

[].method(:inject).parameters

# =\> [[:rest]]

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

Please help…

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [August 22, 2013, 9:02pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297/2 "2013-08-22T21:02:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [August 22, 2013, 9:24pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297/3 "2013-08-22T21:24:49Z")

</div>

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.. 🙂 🙂

Help me to enjoy this awesome Ruby language.. 🙂

> 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

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [August 23, 2013, 7:49pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297/4 "2013-08-23T19:49:15Z")

</div>

On Aug 23, 2013, at 9:57 AM, Love U Ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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]

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [August 23, 2013, 4:57pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297/5 "2013-08-23T16:57:55Z")

</div>

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` ?

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [August 23, 2013, 7:59pm UTC](https://www.ruby-forum.com/t/what-are-rest-or-req-in-ruby/236297/6 "2013-08-23T19:59:57Z")

</div>

tamouse m. wrote in post #1119460:

> On Aug 23, 2013, at 9:57 AM, Love U Ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Ohh! My bad… thanks for the catch! 🙂

> ## (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]
