module GCE::Host::StringUtil

If want sophisticated utility, better to use ActiveSupport

Public Class Methods

camelize(string) click to toggle source
# File lib/gce/host/string_util.rb, line 5
def self.camelize(string)
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }
  string.gsub!(/\//, '::')
  string
end
pluralize(string) click to toggle source
# File lib/gce/host/string_util.rb, line 22
def self.pluralize(string)
  "#{string.chomp('s')}s"
end
singularize(string) click to toggle source
# File lib/gce/host/string_util.rb, line 26
def self.singularize(string)
  string.chomp('s')
end
underscore(camel_cased_word) click to toggle source
# File lib/gce/host/string_util.rb, line 12
def self.underscore(camel_cased_word)
  return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
  word = camel_cased_word.to_s.gsub(/::/, '/')
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end