lib/monkey_patches.rb in zeke-monkey_patches-0.1.11 vs lib/monkey_patches.rb in zeke-monkey_patches-0.1.12

- old
+ new

@@ -1,5 +1,27 @@ +class Object + + unless method_defined? "blank?" + # Snagged from Rails: http://api.rubyonrails.org/classes/Object.html#M000265 + def blank? + respond_to?(:empty?) ? empty? : !self + end + end + + # @person ? @person.name : nil + # vs + # @person.try(:name) + # Snagged from http://ozmm.org/posts/try.html; later incorporated into Rails 2.3 + unless method_defined? "try" + def try(method) + send method if respond_to? method + end + end + + +end + class String # Pollute the space between every letter in a string, # so it will be exempt from any impending string searches. def pollute(delimiter = "^--^--^") @@ -76,10 +98,23 @@ out.pop out << end_string out.join(" ") end end + + def domain + url = self.dup + url=~(/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : nil + end + + def domain_without_www + self.domain.remove_http_and_www + end + + def remove_whitespace + self.gnix("\t").split(" ").remove_blanks.join(" ") + end def replace_wonky_characters_with_ascii t = self.to_s t.gsub!(/&#8211;/, '-') # en-dash t.gsub!(/&#8212;/, '--') # em-dash @@ -179,10 +214,14 @@ end end class Array + + def remove_blanks + self.reject{ |e| e.blank? } + end # Like Array.shift, but returns the array instead of removed the element. def remove_first_element self[1..self.size] end @@ -192,26 +231,5 @@ self[0..self.size-2] end end -class Object - - unless method_defined? "blank?" - # Snagged from Rails: http://api.rubyonrails.org/classes/Object.html#M000265 - def blank? - respond_to?(:empty?) ? empty? : !self - end - end - - # @person ? @person.name : nil - # vs - # @person.try(:name) - # Snagged from http://ozmm.org/posts/try.html; later incorporated into Rails 2.3 - unless method_defined? "try" - def try(method) - send method if respond_to? method - end - end - - -end \ No newline at end of file