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 =