Stefano Crocco
1/24/2007 8:47:00 AM
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