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:
parent
b1e5f5ee81
commit
8e0158b4d7
@ -24,6 +24,15 @@ class String
|
|||||||
end
|
end
|
||||||
end unless method_defined?(:start_with?)
|
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.
|
# String.chomp, but if result is empty: returns nil instead.
|
||||||
# Allows `chuzzle || foo` short-circuits.
|
# Allows `chuzzle || foo` short-circuits.
|
||||||
def chuzzle
|
def chuzzle
|
||||||
|
Loading…
x
Reference in New Issue
Block a user