Looking for a match

A

Ari Brown

Hey,
I'm looking to match a string to another string ANYWHERE in a new
file. For instance, my code looks like such:

if readlines[*] == chosen_one # If there's a match -
next # Boil some brains (try
again)
end

My goal is to do a search through each line in the file's line
(readlines[*]) for the chosen_one.

Will Ruby support the wildcard I used? And if not, what could I do to
fix it? BTW, this is apart of the ruby quiz if that helps.

aRi
-------------------------------------------|
Nietzsche is my copilot
 
Y

yermej

Hey,
I'm looking to match a string to another string ANYWHERE in a new
file. For instance, my code looks like such:

if readlines[*] == chosen_one # If there's a match -
next # Boil some brains (try
again)
end

My goal is to do a search through each line in the file's line
(readlines[*]) for the chosen_one.

Will Ruby support the wildcard I used? And if not, what could I do to
fix it? BTW, this is apart of the ruby quiz if that helps.

aRi

If you've read the lines in as elements of an array:

lines = the_file.readlines
lines.any? {|line| line.match chosen_one} # true if there's a match

If you've read in the lines as one big string:

lines = the_file.read
!lines.match(chosen_one).nil? # true if there's a match
 
A

Aaron Patterson

Hey,
I'm looking to match a string to another string ANYWHERE in a new
file. For instance, my code looks like such:

if readlines[*] == chosen_one # If there's a match -
next # Boil some brains (try
again)
end

You should check out Enumerable#any?. I think that is what you are
looking for. For example:

if readlines.any? { |line| line == chosen_one }
...
end
 

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
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top