.include? for string

C

Chris Chris

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
 
T

Tim Hunter

Chris said:
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

Try

a['|']

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

Joel VanderWerf

Chris said:
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"]]

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.
 
D

David A. Black

Hi --

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?

Maybe you want this:

a.delete_if {|ary| !ary.any? {|string| string.include?('ghi|ghi') } }

or something like that.


David
 
D

David A. Black

Hi --

Chris said:
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"]]

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.

I kind of like this hybrid:
=> [["abc", "def", "ghi|ghi|kl"]]


David
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top