Can you explain reject(&:new_record?)

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

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

On Apr 8, 11:08 am, Christophe D. [email protected]
wrote:

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
tophe

That is one of the few Rails idiom that was useful enough that it got
folded into Ruby. I think it is officially a part of 1.8.7 or 1.9 or
something.

Expanded out, it looks like this:

tasks.reject(&:new_record?).each { … }
tasks.reject { |task| task.new_record? }.each { … }

… meaning that the method new_record? is called for each of the task
passed in by reject. Reject itself will reject anything that evaluates
to true. In other words, that snippet of code filters out any tasks
that has not yet been saved to the database, then calls each on that.
So another application of that:

Tasks.all.map(&:name)
Tasks.all.map { |task| task.name }

Gives you a list of all of those task with their name, which I’ve
found pretty useful when playing around in the console.

For a more advanced discussion, the &:new_record? trick works because
it is essentially converting a Symbol into a Proc using Ruby duck
typing and to_proc, and the resulting Proc gets passed into the
reject.

Ho-Sheng H.
http://hosheng.blogspot.com

On Apr 8, 4:08 pm, Christophe D. [email protected]
wrote:

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

Christophe,

The “&:new_record?” is a Rails idiom. In this example it is a shortcut
for…

tasks.reject{|t| t.new_record?}.each do |task|

Anthony C.

On Thu, Apr 8, 2010 at 10:08 AM, Christophe D. <

Thanks to anyone who replied.
You gave me pretty good explanations
Now I better understand why I’ll always be an amateur (and be delighted
to).

Le 8 avr. 2010 à 18:17, Frederick C. a écrit :