Hi all,
Can someone explain to me in terms of ruby why this works?
Story.find_all_by_user_id(2).collect(&:link)    # 1  method call
This is in ROR console.  link is a property of Story.  The above command
finds all db rows and all their properties (columns) with user_id == 2
and creates an array with only the link property.
This is same as:
Story.find_all_by_user_id(2).collect{|x| x.link}  # 2  block
I understand function #2 with use of a block.
I do not understand function #1 in terms of syntax.  What  is &:link?  I
know it represents a property and could accept that this is the way it
works but I want to understand this in terms of ruby syntax.  Is &:link
a block?  If so it would have to represent x.link.  I do not understand
how this is generated in ruby.
TIA,
Pete
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
They are both equivalent in terms of functionality.
The first is a common rails idiom:
@some_collection.collect(&:id)
or
@some_collection.map(&:id) #I''ve seen this one used more than
collect...
The ampersand syntax is simply telling the interpreter that the symbol
is the block parameter to the method. Internally the to_proc method of
the symbol class is called.
Here''s a reference:
http://apidock.com/rails/Symbol/to_proc
Beware, doing @foo.map(&:id) doesn''t perform as well as @foo.map {
|f|
f.id } for big numbers of foos...
Also note that this is a Rails only feature (ie: try it on plain irb
and it will not work).
On Mar 11, 1:35 pm, Hiro Protagonist <rails-mailing-l...@andreas-
s.net> wrote:> Hi all,
> Can someone explain to me in terms of ruby why this works?
>
> Story.find_all_by_user_id(2).collect(&:link)    # 1  method call
>
> This is in ROR console.  link is a property of Story.  The above command
> finds all db rows and all their properties (columns) with user_id == 2
> and creates an array with only the link property.
>
> This is same as:
> Story.find_all_by_user_id(2).collect{|x| x.link}  # 2  block
>
> I understand function #2 with use of a block.
>
> I do not understand function #1 in terms of syntax.  What  is &:link?
 I
> know it represents a property and could accept that this is the way it
> works but I want to understand this in terms of ruby syntax.  Is &:link
> a block?  If so it would have to represent x.link.  I do not understand
> how this is generated in ruby.
>
> TIA,
> Pete
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hiro Protagonist
2009-Mar-11  18:21 UTC
Re: do not understand collect method use wit property
Cool. Thanks for explanation. Pete Harold wrote:> They are both equivalent in terms of functionality. > > The first is a common rails idiom: > > @some_collection.collect(&:id) > or > @some_collection.map(&:id) #I''ve seen this one used more than > collect... > > The ampersand syntax is simply telling the interpreter that the symbol > is the block parameter to the method. Internally the to_proc method of > the symbol class is called. > > Here''s a reference: > http://apidock.com/rails/Symbol/to_proc > > Beware, doing @foo.map(&:id) doesn''t perform as well as @foo.map { |f| > f.id } for big numbers of foos... > Also note that this is a Rails only feature (ie: try it on plain irb > and it will not work). > > On Mar 11, 1:35�pm, Hiro Protagonist <rails-mailing-l...@andreas--- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---