Matching on a final segment of a string


when matching on the final segment of a string, e.g for a file extension, you could use a regular expression:

> "foo.bar" =~ /.*\.bar$/
# => 0
> "foo.bar.baz" =~ /.*\.bar$/
#=> nil

but of course in ruby there’s a much more expressive way

> "foo.bar".end_with?(".bar")
#=> true
> "foo.bar.baz".end_with?(".bar")
#=> false

you can even pass multiple suffixes as arguments:

> "foo.qux".end_with?("bar", "baz", "qux")
#=> true

oh Ruby how I love you so.

api documentation for end_with?