Class: String
Overview
String extension.
Constant Summary collapse
- NUMERIC =
Holds numeric matcher.
/^\s*-?\d+(?:\.\d+)?\s*$/
Instance Method Summary collapse
-
#first ⇒ String
Returns first character of the string.
-
#gsub_f(from, to = nil, &block) ⇒ String
Replaces all substrings defined by Regexp by complex way.
-
#gsub_f!(from, to = nil, &block) ⇒ String
Performs complex regexp replacing same as #gsub_f, but in place.
-
#last ⇒ String
Returns last character of the string.
-
#map(&block) ⇒ Object
Applies block to each character and returns resultant string.
-
#map!(&block) ⇒ String
Applies block to each character in place.
-
#numeric? ⇒ Boolean
Indicates, string is numeric, so consists of numbers only.
-
#strtr(defs, mode = nil, &block) ⇒ String
Replaces set of substrings by another strings.
-
#strtr!(defs, mode = nil, &block) ⇒ String
Replaces set of substrings by another strings – in place.
-
#to_a ⇒ Array
Converts String to Array of characters.
Instance Method Details
#first ⇒ String
Returns first character of the string.
203 204 205 |
# File 'lib/hash-utils/string.rb', line 203 def first self[0].chr end |
#gsub_f(from, to = nil, &block) ⇒ String
Replaces all substrings defined by Regexp by complex way. It means, it gives to block not only whole match, but submatches too. In fact, emulates PHP’s preg_replace_callback() function. In other ways emulates standard #gsub
.
159 160 161 162 163 164 165 166 167 |
# File 'lib/hash-utils/string.rb', line 159 def gsub_f(from, to = nil, &block) __prepare_gsub_f(from, to, block) do |callback| if to.nil? self.gsub(from, &callback) else self.gsub(from, to) end end end |
#gsub_f!(from, to = nil, &block) ⇒ String
Performs complex regexp replacing same as #gsub_f, but in place. In other ways emulates standard #gsub!
.
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/hash-utils/string.rb', line 181 def gsub_f!(from, to = nil, &block) __prepare_gsub_f(from, to, block) do |callback| if to.nil? self.gsub!(from, &callback) else self.gsub!(from, to) end end self end |
#last ⇒ String
Returns last character of the string.
217 218 219 |
# File 'lib/hash-utils/string.rb', line 217 def last self[-1].chr end |
#map(&block) ⇒ Object
Applies block to each character and returns resultant string.
119 120 121 122 123 124 125 126 |
# File 'lib/hash-utils/string.rb', line 119 def map(&block) buffer = " " * self.length self.length.times do |i| buffer[i] = block.call(self[i]) end return buffer end |
#map!(&block) ⇒ String
Applies block to each character in place. For example see #map.
137 138 139 140 141 142 143 |
# File 'lib/hash-utils/string.rb', line 137 def map!(&block) self.length.times do |i| self[i] = block.call(self[i]) end self end |
#numeric? ⇒ Boolean
Indicates, string is numeric, so consists of numbers only.
26 27 28 29 30 31 32 |
# File 'lib/hash-utils/string.rb', line 26 def numeric? if self.match(self.class::NUMERIC) true else false end end |
#strtr(defs, mode = nil, &block) ⇒ String
Replaces set of substrings by another strings.
It’s equivalent of PHP strtr() function. Supports all objects convertable to String, but in that case you must give him block which specifies how to map keys found in the string back to keys in definition Hash or Array.
If you specify the :flat
mode, definition array items will be treaten as arguments to Hash#[] method while internal conversion Array
to Hash
, so then can then use plain array as base for definitions. See Array#to_h.
62 63 64 65 66 67 68 69 |
# File 'lib/hash-utils/string.rb', line 62 def strtr(defs, mode = nil, &block) if block.nil? block = Proc::new { |s| s } end defs, matcher = __prepare_strtr(defs, mode) self.gsub(matcher) { |s| defs[block.call(s)] } end |
#strtr!(defs, mode = nil, &block) ⇒ String
Replaces set of substrings by another strings – in place. See #strtr for details.
83 84 85 86 87 88 89 90 |
# File 'lib/hash-utils/string.rb', line 83 def strtr!(defs, mode = nil, &block) if block.nil? block = Proc::new { |s| s } end defs, matcher = __prepare_strtr(defs, mode) self.gsub!(matcher) { |s| defs[block.call(s)] } end |
#to_a ⇒ Array
Converts String to Array of characters.
103 104 105 |
# File 'lib/hash-utils/string.rb', line 103 def to_a self.split("") end |