search in array

G

Giuseppe Milo

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
 
S

Stefano Crocco

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
 
M

Martin DeMello

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,757
Latest member
PDIJaclyn

Latest Threads

Top