Search in array

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-forum.com/.

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

array.include? string

ri Array#include?
--------------------------------------------------------- Array#include?
     array.include?(obj) -> true or false

···

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

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

thank you very mutch :wink:
this is that i'm searching

···

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