Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/rango/core_ext.rb
Instance Method Summary collapse
-
#camel_case ⇒ String
Convert to camel case.
-
#snake_case ⇒ String
Convert to snake case.
-
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
-
#to_const_string ⇒ String
Convert a path string to a constant name.
Instance Method Details
#camel_case ⇒ String
Convert to camel case.
"foo_bar".camel_case #=> "FooBar"
35 36 37 38 |
# File 'lib/rango/core_ext.rb', line 35 def camel_case return self if self !~ /_/ && self =~ /[A-Z]+.*/ split('_').map{|e| e.capitalize}.join end |
#snake_case ⇒ String
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
21 22 23 24 25 |
# File 'lib/rango/core_ext.rb', line 21 def snake_case return self.downcase if self =~ /^[A-Z]+$/ self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/ return $+.downcase end |
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
61 62 63 |
# File 'lib/rango/core_ext.rb', line 61 def to_const_path snake_case.gsub(/::/, "/") end |
#to_const_string ⇒ String
Convert a path string to a constant name.
"merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"
48 49 50 |
# File 'lib/rango/core_ext.rb', line 48 def to_const_string gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end |