Class: Porch::CoreExt::String
- Inherits:
-
Object
- Object
- Porch::CoreExt::String
- Defined in:
- lib/porch/core_ext/string.rb
Constant Summary collapse
- CAPITALIZE_SEPARATOR =
' '.freeze
- CLASSIFY_SEPARATOR =
'_'.freeze
- NAMESPACE_SEPARATOR =
'::'.freeze
- UNDERSCORE_DIVISION_TARGET =
'\1_\2'.freeze
- UNDERSCORE_SEPARATOR =
'/'.freeze
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #capitalize ⇒ Object
- #gsub(pattern, replacement = nil, &blk) ⇒ Object
-
#initialize(string) ⇒ String
constructor
A new instance of String.
- #split(pattern, limit = 0) ⇒ Object
- #to_s ⇒ Object
- #underscore ⇒ Object
Constructor Details
#initialize(string) ⇒ String
Returns a new instance of String.
10 11 12 |
# File 'lib/porch/core_ext/string.rb', line 10 def initialize(string) @string = string.to_s end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
45 46 47 |
# File 'lib/porch/core_ext/string.rb', line 45 def ==(other) to_s == other end |
#capitalize ⇒ Object
14 15 16 17 18 |
# File 'lib/porch/core_ext/string.rb', line 14 def capitalize head, *tail = underscore.split(CLASSIFY_SEPARATOR) self.class.new tail.unshift(head.capitalize).join(CAPITALIZE_SEPARATOR) end |
#gsub(pattern, replacement = nil, &blk) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/porch/core_ext/string.rb', line 20 def gsub(pattern, replacement=nil, &blk) if block_given? string.gsub(pattern, &blk) else string.gsub(pattern, replacement) end end |
#split(pattern, limit = 0) ⇒ Object
28 29 30 |
# File 'lib/porch/core_ext/string.rb', line 28 def split(pattern, limit=0) string.split(pattern, limit) end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/porch/core_ext/string.rb', line 32 def to_s string end |
#underscore ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/porch/core_ext/string.rb', line 36 def underscore new_string = gsub(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR) new_string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET) new_string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET) new_string.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET) new_string.downcase! self.class.new new_string end |