0

I'm trying to search a string for match on an array of words. I tested the code by specifying a word and also an input and it correctly finds it, but for some reason I can't get it to work in general. I checked that the input is a String and the words are extracted from the list as well. Any help on this would be appreciated. Thanks.

def self.wordmatch(input, list)
  list.each {|word|
  if (input =~ /#{word}/i)
    puts "#{input} => #{word}"
    return 'MATCH'
  end
  }
end
5
  • Do you want the whole method to return the string 'MATCH' when the first match is found? Do you get any output at all? Probably would be best to paste a working script (including the class this method is defined in, perhaps stripped down). Commented Sep 14, 2010 at 14:21
  • Seems to work fine for me. What values are you passing to the method when it doesn't work? Commented Sep 14, 2010 at 14:23
  • Is the problem you're having that it returns after the first match? Commented Sep 14, 2010 at 14:33
  • As a general rule, and it doesn't matter other than as a "Standard" style you should use do/end rather than braces for a multi-line block. Commented Sep 14, 2010 at 14:43
  • Returning after the first match is the behavior I expect. Commented Sep 14, 2010 at 14:52

3 Answers 3

1

This seems to work OK. What you may be getting confused by is that if there is no match then ruby will return the last evaluated expression, which in this case is the list.

I'm not sure what you are wanting to return here in the case of a failure but this will return true/false

 def self.wordmatch(input, list)
  list.each {|word|
  if (word =~ /#{input}/i)
    puts "#{input} => #{word}"
    return true
  end
  }
  return false
end

The following test code

words = %w[a b c]
matches = %w[a b c d]

matches.each do |match|
    puts "Testing #{match} = #{wordmatch(match, words)}"
end

Produces the following results

Testing a = true
Testing b = true
Testing c = true
Testing d = false

EDIT

Following on from the comments this seems to work exactly as advertised.

def wordmatch(input, list)
  list.each {|word|
  if (input =~ /#{word}/i)
    puts "#{input} => #{word}"
    return 'MATCH'
  end
  }
  return nil
end

list = ["hate", "indifference", "love", "foo"]
input1 = "I love summer"
input2 = "I endure summer"

puts "Testing #{input1} = #{wordmatch(input1, list)}"
puts "Testing #{input2} = #{wordmatch(input2, list)}"

And produces the following results

I love summer => love
Testing I love summer = MATCH
Testing I endure summer = 
Sign up to request clarification or add additional context in comments.

4 Comments

This won't work if words is a "list of strings" as mentioned in the question.
Thanks for your answer. Here is the intended behavior: input = "I love summer", list = ["hate", "indifference", "love", "foo"]. Now calling wordmatch(input, list) should put the matching word in the console and return "MATCH". For some reason I'm not able to get this to work, seems simple enough.
@Mark Redding. Why not? That is exactly what this is doing passing a list of single character strings
sorry bout that. Question seems like it could have used a bit more info.
0

Try this. I think you have them backwards..


def self.wordmatch(input, list)
  list.each {|word|
  if (word =~ /#{input}/i)
    puts "#{input} => #{word}"
    return 'MATCH'
  end
  }
end

http://rubular.com/ is a great site to mess with this stuff.

2 Comments

Mark, I tried this and the end result is still the same. You mentioned that it won't work for "list of strings". Could you explain why?
I read your question such that I thought input="love" , list = ["i love summer", "summer of love", "i hate summer"]
0

I would implement it like this:

def wordmatch(input, list)
  found = input.scan(/\w+/).find {|w| list.include?(w)}
  if found
    puts found
    return "MATCH"
  end
end

It transform your input string to an array of words, and runs Array#find on it to find the first word in the array that is included in the list array.

To make it case insensitive, you might want to add map(&:downcase) before the find if your list contains only lowercase words.

As noted by others, your implementation will return the whole list when a match is not found, since you did not set an explicit return value in that case.

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.