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…
I am very new to this method :
Method#parameters(Class: Method (Ruby 2.0.0))
12.method(“+”).parameters
[].method(:inject).parameters
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…
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 parameterremember: 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
andb
?–
Posted via http://www.ruby-forum.com/.
Foo.new is different method than Foo#initialize:
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
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!
(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]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs