Keyword arguments in Ruby

Discussion in 'Ruby' started by grom, Jan 25, 2004.

  1. grom

    grom Guest

    Is it possbile for Ruby to have keyword arguments added in like in
    Python.

    Quote from Python documentation:

    4.7.2 Keyword Arguments
    Functions can also be called using keyword arguments of the form
    "keyword = value". For instance, the following function:

    def parrot(voltage, state='a stiff', action='voom', type='Norwegian
    Blue'):
    print "-- This parrot wouldn't", action,
    print "if you put", voltage, "Volts through it."
    print "-- Lovely plumage, the", type
    print "-- It's", state, "!"

    could be called in any of the following ways:

    parrot(1000)
    parrot(action = 'VOOOOOM', voltage = 1000000)
    parrot('a thousand', state = 'pushing up the daisies')
    parrot('a million', 'bereft of life', 'jump')

    but the following calls would all be invalid:

    parrot() # required argument missing
    parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
    parrot(110, voltage=220) # duplicate value for argument
    parrot(actor='John Cleese') # unknown keyword
     
    grom, Jan 25, 2004
    #1
    1. Advertising

  2. On Sun, Jan 25, 2004 at 03:09:58PM +0900, grom wrote:
    > Is it possbile for Ruby to have keyword arguments added in like in
    > Python.


    http://www.rubyist.net/~matz/slides/rc2003/mgp00026.html

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    We are using Linux daily to UP our productivity - so UP yours!
    -- Adapted from Pat Paulsen by Joe Sloan
     
    Mauricio Fernández, Jan 25, 2004
    #2
    1. Advertising

  3. il 24 Jan 2004 22:09:53 -0800, (grom) ha
    scritto::

    >Is it possbile for Ruby to have keyword arguments added in like in
    >Python.


    They're going to be added in ruby2.0.

    In the meanwhile you can
    use instance eval to have something like this:
    Parrot.new{action='voom'; type='norwegian blue'} like in the bindings
    for Tk.
    Or use an hash to do something like this:
    Parrot.new:)action=>'voom',:type=>'norwegian_blue')
     
    gabriele renzi, Jan 25, 2004
    #3
  4. il 24 Jan 2004 22:09:53 -0800, (grom) ha
    scritto::

    >Is it possbile for Ruby to have keyword arguments added in like in
    >Python.
    >


    I supposed I answered but my msg didn't show up on comp.lang.ruby.
    Sorry if I double post:

    kwd args are goin to be included in ruby2.0
    Until that you can emulate them via instance_eval like in tk

    Parrot.new{state='a stiff'}
    or explicitly using an hash:
    Parrot.new:)state=>'a stiff')
     
    gabriele renzi, Jan 25, 2004
    #4
  5. Mauricio Fernández wrote:

    > On Sun, Jan 25, 2004 at 03:09:58PM +0900, grom wrote:
    >
    >>Is it possbile for Ruby to have keyword arguments added in like in
    >>Python.

    >
    >
    > http://www.rubyist.net/~matz/slides/rc2003/mgp00026.html


    It might be instructive for the newcomers to note that this slide refers to "visions for the future", not current functionality.
     
    Michael campbell, Jan 25, 2004
    #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. David Konerding
    Replies:
    0
    Views:
    455
    David Konerding
    Sep 10, 2003
  2. Edward Diener
    Replies:
    14
    Views:
    5,126
    Josiah Carlson
    Apr 6, 2004
  3. Replies:
    6
    Views:
    490
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    379
  5. Peter Motzfeldt
    Replies:
    1
    Views:
    176
Loading...

Share This Page