How do you find an item that satisfies multiple conditions?

I know that to find an item that satisfies one condition, it's this:

@items.find {|item| item.product == product }

I thought I could try this to check that an item satisfies multiple
conditions:

@items.find {|item|
item.product == product
item.red == red
item.blue == blue
}

Of course, I'm not getting desirable results from that. Do you know how
I would correctly find an item that satisfies multiple conditions?

···

--
Posted via http://www.ruby-forum.com/.

Hi Bob. The find method returns a new collection including all
elements for which the block returns boolean true. In this case, your
product and red comparisons happen but are not returned from the block
and are ignored. Whaty ou want is ONE boolean expression combining
all of these comparisons:

@items.find{|item|
  item.product == product && item.red == red && item.blue == blue
}

Chad

···

On Mon, Dec 1, 2008 at 6:34 AM, Bob Sanders <small.business.strategy@gmail.com> wrote:

I know that to find an item that satisfies one condition, it's this:

@items.find {|item| item.product == product }

I thought I could try this to check that an item satisfies multiple
conditions:

@items.find {|item|
item.product == product
item.red == red
item.blue == blue
}

Alle Monday 01 December 2008, Bob Sanders ha scritto:

I know that to find an item that satisfies one condition, it's this:

@items.find {|item| item.product == product }

I thought I could try this to check that an item satisfies multiple
conditions:

@items.find {|item|
item.product == product
item.red == red
item.blue == blue
}

Of course, I'm not getting desirable results from that. Do you know how
I would correctly find an item that satisfies multiple conditions?

@items.find{|item| item.product == product && item.red == red && item.blue == blue}

Stefano

Chad and Stefano! Thank you!! =)

That was so quick. Thanks guys!

(Chad, pleasantly surprised you answered..I LOVE your work!)

···

--
Posted via http://www.ruby-forum.com/.

<snip>

Hi Bob. The find method returns a new collection including all
elements for which the block returns boolean true.

I am afraid that you confused Enumerable#find with Enumerable#select
616/137 > ruby -e 'p [*0..9].find{|x| (x%2).zero? }'
0
617/138 > ruby -e 'p [*0..9].select{|x| (x%2).zero? }'
[0, 2, 4, 6, 8]
robert@siena:~/log/ruby 18:56:47
618/139 > ruby -e 'p [*0..9].select{|x| (x%2).zero? && (x%3).zero?}'
[0, 6]
HTH
Robert

···

On Mon, Dec 1, 2008 at 1:40 PM, Chad Fowler <chad@chadfowler.com> wrote:

--
Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert Dober :wink: