0

I am trying to match one or more keywords within a string (str), but I have no luck. My method below is trying to match all keys when I only needed to match any (one or more).

@str = "g stands for girl and b is for boy"

def key1
 %w[a, b, c, d, f, g]
end    

def result
 if @str.include?( key1 )
  puts "happy days"
 else
  puts "bad days"
 end
end

puts result # => bad days

How to make it show "happy days"?

PS: I have no idea what to name this title to. Maybe a mod could rename it?

3
  • It's not clear what you want here, do you want to check if the string in @str has any of the letters in key1? Commented Mar 9, 2015 at 18:58
  • @victorkohl Yes. Sorry if I can't structure my question precisely. Commented Mar 9, 2015 at 19:00
  • maybe you should refactor your case from a str to a boolean, or why are you exactly do this "thing" within a string? Commented Mar 9, 2015 at 22:05

3 Answers 3

3

You're asking if your string includes the Array: ["a", "b", "c", "d", "f", "g"]

What I think you're trying to ask is this: are there any elements in the array that also exist in the string? This is a good use case for Enumerable#any like this:

[2] pry(main)> @str = "g stands for girl and b is for boy"
=> "g stands for girl and b is for boy"
[3] key1 = %w[a b c d f g]
=> ["a", "b", "c", "d", "f", "g"]
[4] pry(main)> key1.any? { |letter| @str.include?(letter) }
=> true

So to refactor your code, it might look like this:

@str = "g stands for girl and b is for boy"

def key1
 %w[a b c d f g]
end    

def result
 if key1.any? { |letter| @str.include?(letter) }
  puts "happy days"
 else
  puts "bad days"
 end
end

Something to note, with %w you don't need to use commas, you can simply separate the letters by a space (as outlined above).

Sign up to request clarification or add additional context in comments.

5 Comments

Sir you are the best! This is what I was looking for but I dont know how to get the .any? to work! Thanks!
Yes that's what I have. You are the best!
Thanks Cary, I cleaned that up.
Me == dog-with-bone. The commas are not optional, because one obtains an incorrect result if they are included.
I think we confused each other, in the future a suggested edit is probably preferred over the back and forth on the comments. I usually agree with what your statements are, I think it would give me a view into your exact point.
1

I can't clearly understand your question, but I suspect you are looking for following:

key1.find { |k| @str.include? k }

2 Comments

You seem to understand it well enough. You could use index as instead of find, but find reads better, but still doesn't read as well as any?, imo.
as bonus you get value as well.
1

I would use a regular expression:

MAGIC_CHARS = %w[a b c d f g]
  #=> ["a", "b", "c", "d", "f", "g"]

def result(str)
  (str =~ /#{MAGIC_CHARS.join('|')}/) ? "happy days" : "bad days"
end

result("g stands for girl and b is for boy")
  #=> "happy days" 
result("nothin' here")
  #=> "bad days" 

Note:

/#{MAGIC_CHARS.join('|')}/
  #=> /a|b|c|d|f|g/ 

You could instead write:

Regexp.union(MAGIC_CHARS.map { |c| /c/ })
  #=> /(?-mix:c)|(?-mix:c)|(?-mix:c)|(?-mix:c)|(?-mix:c)|(?-mix:c)/

or

/[#{MAGIC_CHARS.join('')}]/
  #=> /[abcdfg]/ 

Another way:

def result(str)
  (str.chars & MAGIC_CHARS).any? ? "happy days" : "bad days"
end

result("g stands for girl and b is for boy")
  #=> "happy days" 
result("nothin' here")
  #=> "bad days" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.