On Apr 8, 4:08 pm, Christophe Decaux <[email protected]>
wrote:
> Hi there,
>
> I've been investigating the railscasts episodes which talk about complex 
> forms and I ran across this notation in the example code provided in part 3 
> of the episode.
>
> at some point in a model method, there is this line:
>
> tasks.reject(&:new_record?).each do |task|
>
> I have no problem with the ".each do..." stuff, I guess that "reject" is 
> quite the opposite of find but I quite don't understand the "&:new_record?" 
> notation
> What is the purpose of the &
> Is it some sort of Ruby idiom

That is equivalent to tasks.reject {|t| t.new_record?} - It's
shorthand for when you want to just call one method on every object
yielded by the block (performance wise there are slight differences to
the long hand version on some versions of ruby (before this was made
part of core)). More generally passing an argument prefixed by & to a
method tells the method "you should use this argument as your block",
so for example if you had a proc object you can use that syntax to say
to a method 'when you call yield, call this proc'. Ruby calls to_proc
on such arguments and rails adds a to_proc method on Symbol which
creates an appropriate proc for you

Fred


>
> I have the feeling that it should read "find any task object that is not new" 
> (i.e. created but not saved)
>
> How would you customize it to achieve this goal :
>
> Find the last task object (sorted by id's) that is not new
>
> Thanks a lot in advance
> Christophe

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to