[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

search in array

Josh

1/24/2007 8:36:00 AM

hi all
is there in ruby a method like in_array() php function?

i need to verify if there is a string in an array

--
Posted via http://www.ruby-....

3 Answers

Stefano Crocco

1/24/2007 8:47:00 AM

0

Alle 09:35, mercoledì 24 gennaio 2007, Giuseppe Milo ha scritto:
> hi all
> is there in ruby a method like in_array() php function?
>
> i need to verify if there is a string in an array

I don't know php, so I don't know what in_array() exactly does. At any rate,
the method include? of the Array class tells whether a given element is in
the array:

a=['a', 'b', 'c']
a.include?('a')
=>true
a.include?('d')
=>false

This method uses the == method of the argument to test for equality. If you
need something different, you can use the grep method, which uses === and
returns an array containing the matches (useful, for instance, for regular
expressions), find, which takes a block and returns the first element for
which the block returns true, or any? which works like find but returns a
boolean value indicating whether there was a match:

a=['abc','def']
a.include?('a')
=>false
a.grep(/a/)
=>['abc']

a=[1,2,3]
a.find{|i| i>1}
=>2
a.any?{|i| i>1}
=>true

I hope this helps

Martin DeMello

1/24/2007 8:47:00 AM

0

On 1/24/07, Giuseppe Milo <josh@pixael.com> wrote:
> hi all
> is there in ruby a method like in_array() php function?
>
> i need to verify if there is a string in an array

array.include? string

ri Array#include?
--------------------------------------------------------- Array#include?
array.include?(obj) -> true or false
------------------------------------------------------------------------
Returns +true+ if the given object is present in _self_ (that is,
if any object +==+ _anObject_), +false+ otherwise.

a = [ "a", "b", "c" ]
a.include?("b") #=> true
a.include?("z") #=> false



martin

Josh

1/24/2007 8:50:00 AM

0

thank you very mutch ;-)
this is that i'm searching

--
Posted via http://www.ruby-....