# Looking for a match

**URL:** <https://www.ruby-forum.com/t/looking-for-a-match/107761>\
**Category:** Ruby\
**Created:** [June 26, 2007, 12:44am UTC](https://www.ruby-forum.com/t/looking-for-a-match/107761 "2007-06-26T00:44:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [June 26, 2007, 12:44am UTC](https://www.ruby-forum.com/t/looking-for-a-match/107761/1 "2007-06-26T00:44:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [June 26, 2007, 2:56am UTC](https://www.ruby-forum.com/t/looking-for-a-match/107761/2 "2007-06-26T02:56:06Z")

</div>

On Jun 25, 5:44 pm, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [June 26, 2007, 3:03am UTC](https://www.ruby-forum.com/t/looking-for-a-match/107761/3 "2007-06-26T03:03:07Z")

</div>

On Tue, Jun 26, 2007 at 07:44:14AM +0900, Ari B. 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
