lib/rext/string/helpers.rb in visionmedia-rext-0.0.2 vs lib/rext/string/helpers.rb in visionmedia-rext-0.0.3

- old
+ new

@@ -2,10 +2,34 @@ require 'extlib' class String ## + # Return Base 64 decoded string. + # + # === Examples + # + # 'Y29va2llcw=='.base64_decode # => cookies + # + + def base64_decode + unpack('m').first + end + + ## + # Return Base 64 encoded string. + # + # === Examples + # + # 'cookies'.base64_encode # => Y29va2llcw== + # + + def base64_encode + [self].pack('m').chop + end + + ## # Returns a File instance. # # === Examples # # 'History.rdoc'.file.mtime @@ -65,9 +89,26 @@ # '$100,000'.digitize # => 100000 # def digitize gsub /[^\d]/, '' + end + + ## + # Returns a constant when the string is a valid constant name. + + def constantize + Extlib::Inflection.constantize self + end + + ## + # Convert a string to camel-case, and optionally +capitalize_first_letter+. + + def camelize capitalize_first_letter = false + string = Extlib::Inflection.camelize(self) + return string if capitalize_first_letter + string[0,1] = string.first.downcase + string end ## # Wrap a string with a +prefix+ and optional # +suffix+. When the +suffix+ is not present