Simple Question

Discussion in 'Ruby' started by Joe Blow, Nov 15, 2008.

  1. Joe Blow

    Joe Blow Guest

    I saw the following in a book.

    def deleted_roles=(values)
    roles.find(*values).each(&:destroy)
    end

    I understand that this method finds all the roles with the given ids and
    calls destroy on all of them. My question is, what is the significance
    of *. Why is it roles.find(*values) and not just plain
    roles.find(values)?

    Is there any difference?

    Could it possibly be a typo and the method should be defined as

    def deleted_roles(*values)...

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Blow, Nov 15, 2008
    #1
    1. Advertising

  2. Joe Blow

    Serabe Guest

    * expands an array as multiple arguments. For example, type in an irb session:

    def find(*args) # This * is not the same * as yours, this is just used
    to collect all the arguments in a single array
    args.each {|x| puts x.class}
    end

    find [1,2,3] => Array
    find *[1,2,3] => Fixnum three times.

    Regards,

    Serabe

    --
    http://www.serabe.com
     
    Serabe, Nov 15, 2008
    #2
    1. Advertising

  3. Joe Blow

    Peter Szinek Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi,

    > def deleted_roles=(values)
    > roles.find(*values).each(&:destroy)
    > end
    >
    > I understand that this method finds all the roles with the given ids
    > and
    > calls destroy on all of them. My question is, what is the significance
    > of *. Why is it roles.find(*values) and not just plain
    > roles.find(values)?
    >
    > Is there any difference?
    >
    > Could it possibly be a typo and the method should be defined as
    >
    > def deleted_roles(*values)...


    It looks like roles.find expects a list of parameters (i.e. it should
    be invoked like this: roles.find('admin', 'user', 'editor'))
    but deleted_roles= wants an array (i.e. deleted_rows = ['admin',
    'user', 'editor'])

    The * a.k.a. splat does exactly this conversion - so if someone calls

    deleted_rows = ['admin', 'user', 'editor']

    inside the method

    roles.find('admin', 'user', 'editor').each(&:destroy)

    will be called (note that the brackets are gone)


    HTH,
    Peter
    ___
    http://www.rubyrailways.com
     
    Peter Szinek, Nov 15, 2008
    #3
    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. Replies:
    0
    Views:
    566
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    673
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    846
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    379
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    217
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page