Array.include? with a regex?

S

Sergio Ruiz

i am wondering if this is possible.. i can't seem to figure it out..

i am trying to see if a value is inside an array using regex..

something like:

a = ["pie","cookies","soda","icecream"]

i want to see if something starts with "coo" i should get one..

it doesn't look like i am doing it correctly..


irb(main):010:0> a.include?(/^coo/)
=> false

ideas?

thanks!
 
P

Peter Szinek

[Note: parts of this message were removed to make it a legal post.]


i am wondering if this is possible.. i can't seem to figure it out..

i am trying to see if a value is inside an array using regex..

something like:

a = ["pie","cookies","soda","icecream"]

i want to see if something starts with "coo" i should get one..

it doesn't look like i am doing it correctly..


irb(main):010:0> a.include?(/^coo/)
=> false

ideas?

Enumerable#grep:

a = ["pie","cookies","soda","icecream"]
a.grep /^coo/

Cheers,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org
 
B

Brian Candler

Sergio said:
i am wondering if this is possible.. i can't seem to figure it out..

i am trying to see if a value is inside an array using regex..

something like:

a = ["pie","cookies","soda","icecream"]

i want to see if something starts with "coo" i should get one..

irb(main):001:0> a = ["pie","cookies","soda","icecream"]
=> ["pie", "cookies", "soda", "icecream"]
irb(main):002:0> a.find { |e| /^coo/ =~ e }
=> "cookies"
 

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

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,618
Latest member
Leemorton01

Latest Threads

Top