Forum: Ruby on Rails Can someone explain what is wrong with my syntax?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D299e69ca3532ffad7b12377e5e42dd5?d=identicon&s=25 BushyMark (Guest)
on 2008-12-11 17:00
(Received via mailing list)
Hey Everyone, I think this should be an easy one, and the solution
right now was to use two lines instead of one, but I am curious why
this syntax isn't working.

Assignment.find(:all).delete_if {|assignment|
assignment.assignment_name == "Lunch" || assignment.assignment_name ==
"Lunch"}

This will return the array without the "Lunch", but with the break
still in tact, so I tried again:

Assignment.find(:all).delete_if {|assignment|
assignment.assignment_name == ("Lunch" || "Break")}

Nope! No good, once again, lunch is gone, but break is still in the
array.

tried to do the same thing with select as well . . . no dice. I have
this working right now with two lines of code, but I am really curious
where my syntax is going wrong. More a question of interest then
anything else.

Thanks!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-11 17:10
(Received via mailing list)
On 11 Dec 2008, at 15:59, BushyMark wrote:

> still in tact, so I tried again:
>
> Assignment.find(:all).delete_if {|assignment|
> assignment.assignment_name == ("Lunch" || "Break")}
>
> Nope! No good, once again, lunch is gone, but break is still in the
> array.
>

That doesn't do what you think it does. First it evaluates ("Lunch" ||
"Break") which evaluates to "Lunch", so that just does

Assignment.find(:all).delete_if {|assignment|
assignment.assignment_name == "Lunch"}

you would need

Assignment.find(:all).delete_if do |assignment|
   assignment.assignment_name == "Lunch" || assignment.assignment_name
== "Break"
end

or

Assignment.find(:all).delete_if do |assignment|
   ["Lunch", "Break"].include? assignment.assignment_name
end

Fred
This topic is locked and can not be replied to.