Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 3
    This is a neat trick I've not seen before. But #include? is still a little faster. Commented Jan 20, 2017 at 2:09
  • 5
    #include? doesn't work when you are working with sentences with spaces inside because #include splits the sentence in words and then uses the words as separate array values. This works perfectly for sentences. +1
    – luissimo
    Commented Jan 25, 2019 at 15:11
  • 1
    See Ruby's String [] method for more information. Commented Feb 14, 2020 at 4:01
  • 4
    @luissimo include? does not split the string. It simply checks if a string is or isn't within an other string. 'Hello World! How are you?'.include?('e y') #=> true This answer does the same thing. 'Hello World! How are you?'['e y'] #=> "e y" (which is truthy), nil is returned if there is no match (which is falsy).
    – 3limin4t0r
    Commented Aug 14, 2021 at 11:34