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:
in arr2 using select and grep, i.e.
What is my error or misunderstanding here?
know.
Array#grep returns an array with the matches, which means it returns an
empty
array, which evaluates to true, if there’s no match. You can do what you
want
with
Because grep returns an empty array when it can’t find the
given string. And an empty array (everything except false and nil,
actually) is true in Ruby. Use #include? instead:
Because grep returns an empty array when it can’t find the
given string. And an empty array (everything except false and nil,
actually) is true in Ruby. Use #include? instead:
arr1.select { |y| arr2.include?(y) }
No, that’s not the same.
array = %w{ one two three }
=> [“one”, “two”, “three”]
arr1.select { |y| arr2.grep(y) }
because I am testing this expression prior to using it to find objects
–
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
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:
So since the code in question only deals with strings and not regexen,
using
include? does indeed have the same result as checking whether grep
returned
an empty array.
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.
Everything in ruby can be used as a boolean value, so technically you
can use
it. The only problem is that the value grep returns is always a ‘true’
value.
=> [“one”, “two”, “three”]
=> [“two”, “three”]
given string. And an empty array (everything except false and nil,
actually) is true in Ruby. Use #include? instead:
arr1.select { |y| arr2.include?(y) }
No, that’s not the same.
=> false
Given Milo’s use case, he’s only using strings, so it doesn’t
matter. include? is clearer in this case, IMO.
–
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
It all depends on what you need. If you need Set semantics, use Sets.
If you need lists (maintain a particular order) use Arrays. It all
depends. You can easily convert via #to_a and #to_set. But if you are
finding yourself converting all the time chances are that you are
doing something wrong.
Just one additional remark: if your Arrays grow large you might
benefit from converting them to Set which has faster lookups.
You’re right - if I use a Set then the lookup is astonishingly fast by
comparison.
Is it OK to combine arrays and sets, though? For example, I have a Set
of things I need to grep and an array I don’t, and these need to be
combined as an array. So, is:
So since the code in question only deals with strings and not regexen, using
include? does indeed have the same result as checking whether grep returned
an empty array.
I didn’t say they were the same; I said that grep() didn’t work in
that context, but include?() did.
I think that was in reply to me, and it’s true that I was using “not
the same” to mean “not interchangeable” – but in fact I was all wrong
anyway because I rewrote the OP’s code in my head to use regexes
instead of strings.
David
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.