Class: StringCases
- Inherits:
-
Object
- Object
- StringCases
- Defined in:
- lib/string-cases.rb
Class Method Summary collapse
- .camel_to_snake(str) ⇒ Object
- .constantize(str) ⇒ Object
- .pluralize(str) ⇒ Object
- .singularize(str) ⇒ Object
- .snake_to_camel(str) ⇒ Object
Class Method Details
.camel_to_snake(str) ⇒ Object
6 7 8 |
# File 'lib/string-cases.rb', line 6 def self.camel_to_snake(str) str.to_s.gsub(/(.)([A-Z])/, '\1_\2').downcase end |
.constantize(str) ⇒ Object
34 35 36 37 38 |
# File 'lib/string-cases.rb', line 34 def self.constantize(str) str.to_s.split("::").inject(Module) do |mod_path, mod_to_find| mod_path.const_get(mod_to_find) end end |
.pluralize(str) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/string-cases.rb', line 10 def self.pluralize(str) str = "#{str}" if str.match(/([^aeiouy]|qu)y$/i) str = str.gsub(/y\Z/, "ies") else str << "s" end str end |
.singularize(str) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/string-cases.rb', line 22 def self.singularize(str) str = "#{str}" if str.end_with?("ies") str = str.gsub(/ies\Z/, "y") else str = str.gsub(/s\Z/, "") end str end |
.snake_to_camel(str) ⇒ Object
2 3 4 |
# File 'lib/string-cases.rb', line 2 def self.snake_to_camel(str) str.to_s.split("_").map(&:capitalize).join("") end |