From 8e0158b4d74b3724d31cd284ce362b07f5678d9f Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Fri, 19 Jul 2013 22:32:56 -0500 Subject: [PATCH] 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. --- Library/Homebrew/extend/string.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Library/Homebrew/extend/string.rb b/Library/Homebrew/extend/string.rb index b2a247297b..7fe47d38e4 100644 --- a/Library/Homebrew/extend/string.rb +++ b/Library/Homebrew/extend/string.rb @@ -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