Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simple Question

Reply
Thread Tools

Simple Question

 
 
Joe Blow
Guest
Posts: n/a
 
      11-15-2008
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/.

 
Reply With Quote
 
 
 
 
Serabe
Guest
Posts: n/a
 
      11-15-2008
* 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

 
Reply With Quote
 
 
 
 
Peter Szinek
Guest
Posts: n/a
 
      11-15-2008
[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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments