# .include? for string

**URL:** <https://www.ruby-forum.com/t/include-for-string/142336>\
**Category:** Ruby\
**Created:** [July 6, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/include-for-string/142336 "2008-07-06T23:46:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 6, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/include-for-string/142336/1 "2008-07-06T23:46:19Z")

</div>

Hi,

I have an array a

=\> [[“abc”, “def”, “ghi|ghi|kl”], [“jkl”, “mno”, “ghi|pqr”], [“stu”,  
“vwx”, “yz|abc”]]

Some strings contain the pipe character.

I want to delete all the arrows that do NOT contain a certain string.

I tried this:

a.delete\_if { |x| !x.include? “ghi|ghi” }

=\> []

Of course, the array I need would be  
=\> [[“abc”, “def”, “ghi|ghi|kl”]]

I couldn’t figure out a work around. Any wildcards available for string  
checking in Ruby? Any ideas?  
Thanks.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 7, 2008, 12:04am UTC](https://www.ruby-forum.com/t/include-for-string/142336/2 "2008-07-07T00:04:49Z")

</div>

Chris C. wrote:

> I couldn’t figure out a work around. Any wildcards available for string  
> checking in Ruby? Any ideas?  
> Thanks.
> 
> Cheers,  
> Chris

Try

a[’|’]

That will return nil if the string does not contain the pipe.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 7, 2008, 12:05am UTC](https://www.ruby-forum.com/t/include-for-string/142336/3 "2008-07-07T00:05:24Z")

</div>

Chris C. wrote:

> I tried this:
> 
> a.delete\_if { |x| !x.include? “ghi|ghi” }
> 
> =\> []
> 
> Of course, the array I need would be  
> =\> [[“abc”, “def”, “ghi|ghi|kl”]]

Enumerable#grep can be helpful here.

a = [[“abc”, “def”, “ghi|ghi|kl”], [“jkl”, “mno”, “ghi|pqr”], [“stu”,  
“vwx”, “yz|abc”]]

a.delete\_if { |x| x.grep(/ghi|ghi/).empty? }

p a # ==\> [[“abc”, “def”, “ghi|ghi|kl”]]

Note that the ‘|’ char needs to be escaped in a regex.

Another option:

a.delete\_if { |x| x.all? { |s| not s.include? “ghi|ghi” }}

Closer to the way you phrased the problem, but IMO a little less  
rubyesque.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 7, 2008, 12:05am UTC](https://www.ruby-forum.com/t/include-for-string/142336/4 "2008-07-07T00:05:59Z")

</div>

Hi –

On Mon, 7 Jul 2008, Chris C. wrote:

> I couldn’t figure out a work around. Any wildcards available for string  
> checking in Ruby? Any ideas?

Maybe you want this:

a.delete\_if {|ary| !ary.any? {|string| string.include?(‘ghi|ghi’) } }

or something like that.

David

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 7, 2008, 12:47am UTC](https://www.ruby-forum.com/t/include-for-string/142336/5 "2008-07-07T00:47:07Z")

</div>

Hi,

thank you for your suggestions… Problem solved. Cheers, CHris

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [July 7, 2008, 12:13am UTC](https://www.ruby-forum.com/t/include-for-string/142336/6 "2008-07-07T00:13:00Z")

</div>

Hi –

On Mon, 7 Jul 2008, Joel VanderWerf wrote:

> > I want to delete all the arrows that do NOT contain a certain string.  
> > Enumerable#grep can be helpful here.  
> > Another option:
> 
> a.delete\_if { |x| x.all? { |s| not s.include? “ghi|ghi” }}
> 
> Closer to the way you phrased the problem, but IMO a little less rubyesque.

I kind of like this hybrid:

> > def a.delete\_unless(&block); replace select(&block); end  
> > =\> nil
> 
> > a.delete\_unless {|ary| ary.include? {|string| string.include?(“ghi|ghi”) }}  
> > =\> [[“abc”, “def”, “ghi|ghi|kl”]]

David
