Set operation

Hi,

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 :frowning:

Thanks for help!
Shannon

···

MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus

I don’t know what ‘in’ is used for, but another way to do this might be
if [array_of_strings].index(myString) then…

String#index returns nil if myString is not in array_of_strings.

Tim Bates

···

On Wed, 27 Nov 2002 09:41 pm, Shannon Fang wrote:

if ([string] & array_of_strings).length!=0 then…


tim@bates.id.au

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?

used by `for'

      http://www.ruby-lang.org/en/man-1.4/syntax.html#for

Guy Decoux

Hi,

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)

If array_of_strings is large and invariant, you may want to
prepare a hash whose keys are the elements.

hash_of_srings = {}
array_of_strings.each {|s| hash_of_srings[s] = true}

if hash_of_srings[myString]

Array#index and Array#include? are O(n) order but Hash# is
O(1).

···

At Wed, 27 Nov 2002 20:18:15 +0900, Tim Bates wrote:


Nobu Nakada

Hi,

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 ==.

Maybe it’s just me.

Gavin

···

From: nobu.nokada@softhome.net

At Wed, 27 Nov 2002 20:18:15 +0900, > Tim Bates wrote:

Hi –

···

On Wed, 27 Nov 2002, Gavin Sinclair wrote:

From: nobu.nokada@softhome.net

Hi,

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.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav