Class: RichString
- Inherits:
-
Object
- Object
- RichString
- Defined in:
- lib/brut/junk_drawer.rb
Overview
A wrapper around a string to avoid adding a ton of methods to String.
This may not survive to 1.0 - use at your own risk.
Class Method Summary collapse
-
.from_string(string, blank_is_nil: true) ⇒ RichString|nil
Create a RichString, return
nilinstead for blank strings or nil.
Instance Method Summary collapse
-
#+(other) ⇒ Object
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
-
#initialize(string) ⇒ RichString
constructor
Create a RichString.
-
#length ⇒ Object
-
#to_s ⇒ Object
-
#to_s_or_nil ⇒ Object
-
#to_str ⇒ Object
-
#to_sym ⇒ Object
Constructor Details
#initialize(string) ⇒ RichString
Create a RichString. This calls to_s on its argument.
78 79 80 |
# File 'lib/brut/junk_drawer.rb', line 78 def initialize(string) @string = string.to_s end |
Class Method Details
.from_string(string, blank_is_nil: true) ⇒ RichString|nil
Create a RichString, return nil instead for blank strings or nil
64 65 66 67 68 69 70 71 72 |
# File 'lib/brut/junk_drawer.rb', line 64 def self.from_string(string,blank_is_nil:true) if string.nil? return nil end if string.to_s.strip == "" && blank_is_nil return nil end self.new(string) end |
Instance Method Details
#+(other) ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/brut/junk_drawer.rb', line 170 def +(other) if other.kind_of?(RichString) RichString.new(self.to_s + other.to_s) elsif other.kind_of?(String) self.to_s + other else super(other) end end |
#<=>(other) ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/brut/junk_drawer.rb', line 160 def <=>(other) if other.kind_of?(RichString) self.to_s <=> other.to_s elsif other.kind_of?(String) self.to_s <=> other else super end end |
#==(other) ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/brut/junk_drawer.rb', line 150 def ==(other) if other.kind_of?(RichString) self.to_s == other.to_s elsif other.kind_of?(String) self.to_s == other else false end end |
#length ⇒ Object
145 |
# File 'lib/brut/junk_drawer.rb', line 145 def length = to_s.length |
#to_s ⇒ Object
143 |
# File 'lib/brut/junk_drawer.rb', line 143 def to_s = @string |
#to_s_or_nil ⇒ Object
148 |
# File 'lib/brut/junk_drawer.rb', line 148 def to_s_or_nil = @string.to_s.strip.empty? ? nil : self.to_s |
#to_str ⇒ Object
144 |
# File 'lib/brut/junk_drawer.rb', line 144 def to_str = self.to_s |
#to_sym ⇒ Object
146 |
# File 'lib/brut/junk_drawer.rb', line 146 def to_sym = to_s.to_sym |