Module: GCE::Host::StringUtil
- Defined in:
- lib/gce/host/string_util.rb
Overview
If want sophisticated utility, better to use ActiveSupport
Class Method Summary collapse
- .camelize(string) ⇒ Object
- .pluralize(string) ⇒ Object
- .singularize(string) ⇒ Object
- .underscore(camel_cased_word) ⇒ Object
Class Method Details
.camelize(string) ⇒ Object
5 6 7 8 9 10 |
# 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) ⇒ Object
22 23 24 |
# File 'lib/gce/host/string_util.rb', line 22 def self.pluralize(string) "#{string.chomp('s')}s" end |
.singularize(string) ⇒ Object
26 27 28 |
# File 'lib/gce/host/string_util.rb', line 26 def self.singularize(string) string.chomp('s') end |
.underscore(camel_cased_word) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# 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 |