Default arguments problem

  • Thread starter Amir Ebrahimifard
  • Start date
A

Amir Ebrahimifard

Hi
How can I have a method with 3 arguments so that only second argument is
a default
argument for example:

def sum(p,q=2,r)
puts p+q+r
end

(I want to use from this method with only 2 arguments : first argument
and third argument )
 
J

Jason Watson

def sum(p,q=2,r)
puts p+q+r
end

Does the default arg have to be the second one? If not just move it to
the end of the args list.

def sum(p,r, q=2)
puts p+q+r
end
 
J

Joel VanderWerf

Amir said:
Hi
How can I have a method with 3 arguments so that only second argument is
a default
argument for example:

def sum(p,q=2,r)
puts p+q+r
end

(I want to use from this method with only 2 arguments : first argument
and third argument )

Only with ruby 1.9:

$ irb196
=> nil6
=> nil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top