Pardee, Roy
2008-Jul-25 22:16 UTC
Array#reject vs Array#each + in-block boolean test--I''m baffled--please help!
Hey All,
Can someone explain the difference between these two constructs?
Consider a Projects model, which has_many :tasks--so tasks here is (I
believe) an array.
Construct #1:
tasks.reject(&:new_record?).each do |task|
# do some stuff with task
end
Construct #2:
tasks.each do |task|
unless task.new_record?
# do some stuff with task
end
end
I had thought 2 was just a more verbose version of 1, but it seems
that''s not the case. I grabbed #1 from here:
http://railscasts.com/episodes/75 (the existing_task_attributesmethod). I used
construct #2 in my app, and found that not every task
was getting processed.
(Specifically, if the user removed every task on the view and then
saved, all but one would be deleted.)
Reading the one line of documentation for Array#reject, I don''t
understand why these 2 constructs should behave differently.
Many thanks!
-Roy
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---