Class: String
Instance Method Summary collapse
-
#camel_case ⇒ String
Convert to camel case.
-
#colorize ⇒ String
Transform self to
ColoredString. - #remove(pattern) ⇒ Object
- #remove!(pattern) ⇒ Object
-
#snake_case ⇒ String
Convert to snake case.
-
#titlecase ⇒ String
Transform self into titlecased string.
-
#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"
58 59 60 61 |
# File 'lib/rubyexts/string.rb', line 58 def camel_case return self if self !~ /_/ && self =~ /[A-Z]+.*/ split('_').map{|e| e.capitalize}.join end |
#colorize ⇒ String
Transform self to ColoredString
21 22 23 24 |
# File 'lib/rubyexts/string.rb', line 21 def colorize require_relative "colored_string" ColoredString.new(self) end |
#remove(pattern) ⇒ Object
26 27 28 |
# File 'lib/rubyexts/string.rb', line 26 def remove(pattern) self.gsub(pattern, "") end |
#remove!(pattern) ⇒ Object
30 31 32 |
# File 'lib/rubyexts/string.rb', line 30 def remove!(pattern) self.gsub!(pattern, "") end |
#snake_case ⇒ String
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
44 45 46 47 48 |
# File 'lib/rubyexts/string.rb', line 44 def snake_case return self.downcase if self =~ /^[A-Z]+$/ self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/ return $+.downcase end |
#titlecase ⇒ String
Transform self into titlecased string.
11 12 13 |
# File 'lib/rubyexts/string.rb', line 11 def titlecase self.gsub(/\b./) { $&.upcase } end |
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
84 85 86 |
# File 'lib/rubyexts/string.rb', line 84 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"
71 72 73 |
# File 'lib/rubyexts/string.rb', line 71 def to_const_string gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end |