Nice to mee you all here. I’m new to ruby although I have learned many
languages. I do think Ruby is the easiest language I’ve learned, yet it is
impressively powerful.
Here I have a question to ask for help. I tried to test if a string is in an
array of strings, I used this:
if ([string] & array_of_strings).length!=0 then…
It worked. But I don’t like it at all. I tried to use
if string in array_of_strings then …
But it seems not working. My question is, what is the keyword “in” used for?
I checked the help file, no section in there explained the reserved words
if ([string] & array_of_strings).length!=0 then....
Well, you can perhaps use Array#include?
pigeon% ri Array#include?
--------------------------------------------------------- Array#include?
arr.include?( anObject ) -> true or false
···
------------------------------------------------------------------------
Returns true if the given object is present in arr (that is, if any
object == anObject), false otherwise.
a = [ "a", "b", "c" ]
a.include?("b") #=> true
a.include?("z") #=> false
pigeon%
But it seems not working. My question is, what is the keyword "in" used for?
At Wed, 27 Nov 2002 20:18:15 +0900, > > Tim Bates wrote:
if ([string] & array_of_strings).length!=0 then…
I don’t know what ‘in’ is used for, but another way to do this might be
if [array_of_strings].index(myString) then…
Or:
array_of_strings.include?(myString)
I’ve often thought that Array#=== should do the same thing as Array#include?
It seems wasted to be the same as ==.
Don’t forget that === is the case equality operator, so that would
mean that case statements involving arrays were testing for element
inclusion instead of array equivalence.