From: gabriele renzi Date: 2005-12-31T20:07:48+09:00 Subject: Re: Optional arguments and default values Surgeon ha scritto: > Hi, > > How do I give a method optional arguments and default values? >> def f(a=1,b=2) >> a+b >> end => nil >> f => 3 >> f 2 => 4 >> f 2,3 => 5 > Exmpl: > > foo is a function that multiplies all of its arguments together. If > there is not any argument, a default value of "qwerty" returns. > > foo(2,3) ----> 6 > foo(2,3,5) ----> 30 > foo(2,3,5,2) -> 60 > > foo() -----------> "qwerty" maybe you want to get all the "rest" arguments with the "*" declaration in the method like this: >> def foo(*args) # args are put in an array named args >> return "qwerty" if args.empty? >> args.inject do |acc,elem| ?> acc*elem >> end >> end => nil >> foo => "qwerty" >> foo 1,2,3,4 => 24 But I suggest you take a look at some ruby tutorial, this are basic things.