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
nil
instead 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
Constructor Details
#initialize(string) ⇒ RichString
Create a RichString. This calls to_s
on its argument.
77 78 79 |
# File 'lib/brut/junk_drawer.rb', line 77 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
63 64 65 66 67 68 69 70 71 |
# File 'lib/brut/junk_drawer.rb', line 63 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
168 169 170 171 172 173 174 175 176 |
# File 'lib/brut/junk_drawer.rb', line 168 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
158 159 160 161 162 163 164 165 166 |
# File 'lib/brut/junk_drawer.rb', line 158 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
148 149 150 151 152 153 154 155 156 |
# File 'lib/brut/junk_drawer.rb', line 148 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
144 |
# File 'lib/brut/junk_drawer.rb', line 144 def length = to_s.length |
#to_s ⇒ Object
142 |
# File 'lib/brut/junk_drawer.rb', line 142 def to_s = @string |
#to_s_or_nil ⇒ Object
146 |
# File 'lib/brut/junk_drawer.rb', line 146 def to_s_or_nil = @string.to_s.strip.empty? ? nil : self.to_s |
#to_str ⇒ Object
143 |
# File 'lib/brut/junk_drawer.rb', line 143 def to_str = self.to_s |