Search code examples
rubycase-sensitive

Ruby - find keywords despite case-sensitive


u = gets.chomp
if u.include? "tree"
  # ...

in this code ruby will search for tree word, but it will ignore all other cases like Tree or tReE etc

is there a way to tell ruby that i don't care about case and catch all the words despite their cases?


Solution

  • You have (at least) two solutions to this problem :

    • downcase u and compare with 'tree' u.downcase.include? 'tree'

    • use case-insensitive regex u.match(/tree/i)

    Bonus specs :

    • Is 'treetop' supposed to match ?

    If not, use the regex /\btree\b/i or scan your sentence for the word tree like so:

    u.downcase.scan(/\w+/).include?('tree')