Forum: Ruby Looking for a match

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
425cab08658a06567879717de154552c?d=identicon&s=25 Ari Brown (Guest)
on 2007-06-26 00:44
(Received via mailing list)
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
Bc8e3e2f7acb3a7d93c10e235dc0b4e1?d=identicon&s=25 yermej@gmail.com (Guest)
on 2007-06-26 02:56
(Received via mailing list)
On Jun 25, 5:44 pm, Ari Brown <a...@aribrown.com> wrote:
> (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
Be30361bb0b0c495e3077db43ad84b56?d=identicon&s=25 Aaron Patterson (Guest)
on 2007-06-26 03:03
(Received via mailing list)
On Tue, Jun 26, 2007 at 07:44:14AM +0900, Ari Brown wrote:
> 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
This topic is locked and can not be replied to.