Skip to content

Commit

Permalink
Add String#end_with?
Browse files Browse the repository at this point in the history
I'm tired of not remembering if start_with?/end_with? are portable, so
just add them both if they're not defined.
  • Loading branch information
jacknagel committed Jul 20, 2013
1 parent 57ad4d4 commit b2c50ef
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Library/Homebrew/extend/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def start_with?(*prefixes)
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
Expand Down

0 comments on commit b2c50ef

Please sign in to comment.