From: shadowfirebird@... Date: 2008-08-01T22:01:28+09:00 Subject: Re: Using array.select with grep Someone please correct me if I'm barking up the wrong tree, but the block in select wants a function that returns a logical, and grep isn't one. It seems to work if you use include?(), instead, though. irb(main):004:0> a1 = [ "one", "two", "three" ] => ["one", "two", "three"] irb(main):005:0> a2 = ["two", "three", "four" ] => ["two", "three", "four"] irb(main):006:0> a1.find_all{|x| a2.grep(x)} => ["one", "two", "three"] irb(main):007:0> a1.find_all{|x| a2.grep(x) == true} => [] irb(main):007:0> a1.find_all{|x| a2.include?(x)} => ["two", "three"] On 8/1/08, Milo Thurston wrote: > Using irb I set up the following arrays: > >>> arr1 > => ["one", "two", "three"] >>> arr2 > => ["two", "three", "four"] > > I would expect to be able to collect the elements of arr1 that are also > in arr2 using select and grep, i.e. > >>> arr1.select { |y| arr2.grep(y) } > => ["two", "three"] > > But what I actually get is: > >>> arr1.select { |y| arr2.grep(y) } > => ["one", "two", "three"] > > What is my error or misunderstanding here? > > Before anyone asks, I'm not using the array intersecttion (arr1 & arr2) > because I am testing this expression prior to using it to find objects > in an array (arr1) where an object parameter is found in an array of > strings (arr2), e.g. to find all people from the array of people objects > whose first name is in a list of names: > > arr1.select { |y| arr2.grep(y.string_of_interest) } > > Although if there's a better way of doing that I'd be interested to > know. > -- > Posted via http://www.ruby-forum.com/. > > -- Me, I imagine places that I have never seen / The colored lights in fountains, blue and green / And I imagine places that I will never go / Behind these clouds that hang here dark and low But it's there when I'm holding you / There when I'm sleeping too / There when there's nothing left of me / Hanging out behind the burned-out factories / Out of reach but leading me / Into the beautiful sea