M
Milo Thurston
Using irb I set up the following arrays:
I would expect to be able to collect the elements of arr1 that are also
in arr2 using select and grep, i.e.
=> ["two", "three"]
But what I actually get is:
=> ["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.
=> ["two", "three", "four"]arr1 => ["one", "two", "three"]
arr2
I would expect to be able to collect the elements of arr1 that are also
in arr2 using select and grep, i.e.
=> ["two", "three"]
But what I actually get is:
=> ["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.