Optional arguments and default values

Discussion in 'Ruby' started by Surgeon, Dec 30, 2005.

  1. Surgeon

    Surgeon Guest

    Hi,

    How do I give a method optional arguments and default values?

    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"
     
    Surgeon, Dec 30, 2005
    #1
    1. Advertising

  2. 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.
     
    gabriele renzi, Dec 30, 2005
    #2
    1. Advertising

  3. Surgeon

    Ross Bamford Guest

    On Fri, 30 Dec 2005 19:59:05 -0000, Surgeon <> wrote:

    > Hi,
    >
    > How do I give a method optional arguments and default values?
    >
    > 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:
    def foo(*args)
    args.empty? && "qwerty" or args.inject(0) { |s,i| s * i }
    end

    or:

    def foo(*args)
    if args.empty?
    "qwerty"
    else
    args.inject(0) { |s,i| s * i }
    end
    end

    Default values are slightly different:

    def sum(v1 = 10, v2 = 5)
    v1 + v2
    end

    Once you give a default value to an arg, you must also give defaults to
    all following args (except any &block arg).

    Defaults don't have to be literal - you can use anything

    def foo(arg = somemethod('c')) ... end

    even another arg

    def foo(a1, a2 = a1) ... end

    I believe they're evaluated in the scope of the method itself. It's quite
    cool.

    Cheers,

    --
    Ross Bamford -
     
    Ross Bamford, Dec 30, 2005
    #3
  4. Ross Bamford <> wrote:
    > On Fri, 30 Dec 2005 19:59:05 -0000, Surgeon <>
    > wrote:
    >> Hi,
    >>
    >> How do I give a method optional arguments and default values?
    >>
    >> 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:
    > def foo(*args)
    > args.empty? && "qwerty" or args.inject(0) { |s,i| s * i }
    > end
    >
    > or:
    >
    > def foo(*args)
    > if args.empty?
    > "qwerty"
    > else
    > args.inject(0) { |s,i| s * i }
    > end
    > end


    The implementation of Enumerable#inject allows for an even more elegant
    solution

    def foo(*args)
    args.inject {|a,b| a*b} || "qwerty"
    end

    >> foo 2,3,5

    => 30
    >> foo 2,3

    => 6
    >> foo 2

    => 2
    >> foo

    => "qwerty"

    Note: if args is empty this inject returns nil, if there is just one element
    that is returned.

    Another solution would be to implement things like this in Enumerable:

    module Enumerable
    def sum() inject(0) {|a,b| a+b} end
    def product() inject(1) {|a,b| a*b} end
    end

    or

    module Enumerable
    def sum() inject {|a,b| a+b} end
    def product() inject {|a,b| a*b} end
    end

    Allowing for invocations like these

    >> [1,2,3,4].sum

    => 10
    >> (1..4).sum

    => 10
    >> (1...5).sum

    => 10
    >> (1..10).map { rand 20 }.sum

    => 91

    Kind regards

    robert
     
    Robert Klemme, Dec 31, 2005
    #4
  5. Surgeon

    Ross Bamford Guest

    On Sat, 31 Dec 2005 12:58:14 -0000, Robert Klemme <> wrote:

    > Ross Bamford <> wrote:
    >> Maybe:
    >> def foo(*args)
    >> args.empty? && "qwerty" or args.inject(0) { |s,i| s * i }
    >> end
    >>
    >> or:
    >>
    >> def foo(*args)
    >> if args.empty?
    >> "qwerty"
    >> else
    >> args.inject(0) { |s,i| s * i }
    >> end
    >> end

    >
    > The implementation of Enumerable#inject allows for an even more elegant
    > solution
    >
    > def foo(*args)
    > args.inject {|a,b| a*b} || "qwerty"
    > end
    >
    >>> foo 2,3,5

    > => 30
    >>> foo 2,3

    > => 6
    >>> foo 2

    > => 2
    >>> foo

    > => "qwerty"
    >
    > Note: if args is empty this inject returns nil, if there is just one
    > element that is returned.
    >


    Cool, I didn't know that, thanks for pointing it out :)

    Cheers,

    --
    Ross Bamford -
     
    Ross Bamford, Dec 31, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Probert

    optional arguments in c extensions

    Paul Probert, Oct 24, 2003, in forum: Python
    Replies:
    1
    Views:
    267
    Paul Probert
    Oct 24, 2003
  2. Roger Upole
    Replies:
    0
    Views:
    413
    Roger Upole
    Dec 1, 2003
  3. Edward Diener
    Replies:
    14
    Views:
    5,034
    Josiah Carlson
    Apr 6, 2004
  4. tutmann
    Replies:
    4
    Views:
    449
  5. wyo
    Replies:
    5
    Views:
    120
    scripts.contact
    Apr 28, 2007
Loading...

Share This Page