Alle 09:35, mercoled=C3=AC 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=
,=20
the method include? of the Array class tells whether a given element is in=
=20
the array:
a=3D['a', 'b', 'c']
a.include?('a')
=3D>true
a.include?('d')
=3D>false
This method uses the =3D=3D method of the argument to test for equality. If=
you=20
need something different, you can use the grep method, which uses =3D=3D=3D=
and=20
returns an array containing the matches (useful, for instance, for regular=
=20
expressions), find, which takes a block and returns the first element for=20
which the block returns true, or any? which works like find but returns a=20
boolean value indicating whether there was a match:
a=3D['abc','def']
a.include?('a')
=3D>false
a.grep(/a/)
=3D>['abc']
a=3D[1,2,3]
a.find{|i| i>1}
=3D>2
a.any?{|i| i>1}
=3D>true
I hope this helps