Add String#end_with?

I'm tired of not remembering if start_with?/end_with? are portable, so
just add them both if they're not defined.
This commit is contained in:
Jack Nagel 2013-07-19 22:32:56 -05:00
parent b1e5f5ee81
commit 8e0158b4d7

View File

@ -24,6 +24,15 @@ class String
end
end unless method_defined?(:start_with?)
def end_with?(*suffixes)
suffixes.any? do |suffix|
if suffix.respond_to?(:to_str)
suffix = suffix.to_str
self[-suffix.length, suffix.length] == suffix
end
end
end unless method_defined?(:end_with?)
# String.chomp, but if result is empty: returns nil instead.
# Allows `chuzzle || foo` short-circuits.
def chuzzle