Class: Hash
Overview
Hash extension.
Class Method Summary collapse
-
.combine(keys, values) ⇒ Hash
Combines two arrays to Hash.
-
.create(default = nil, hash = { }, &block) ⇒ Hash
Creates hash by setting default settings in one call.
-
.define(values = { }, default = nil, &block) ⇒ Hash
Defines hash by setting the default value or an
Proc
and content.
Instance Method Summary collapse
-
#all?(&block) ⇒ Boolean
deprecated
Deprecated.
(since 0.10.0, conflict with built-in method)
-
#all_pairs?(&block) ⇒ Boolean
Checks, all elements follow condition expressed in block.
-
#asort(&block) ⇒ Hash
Sorts hash according to values.
-
#asort!(&block) ⇒ Hash
Sorts hash according to values, replaces the original one.
-
#compact ⇒ Hash
Returns a copy of
self
with allnil
elements removed. -
#compact! ⇒ Hash
Removes
nil
elements from the hash. -
#has_all?(keys) ⇒ Boolean
(also: #has_keys?)
Indicates, all of the keys are available in Hash.
-
#has_some?(keys) ⇒ Boolean
Indicates, some of the keys are available in Hash.
-
#keys_to_sym ⇒ Hash
Converts all keys to symbols.
-
#keys_to_sym! ⇒ Hash
Emulates #keys_to_sym on place.
-
#ksort(&block) ⇒ Hash
Sorts hash according to keys.
-
#ksort!(&block) ⇒ Hash
Sorts hash according to keys, replaces the original one.
-
#map_keys(&block) ⇒ Hash
(also: #collect_keys)
Returns a new hash with the results of running block once for every key in
self
. -
#map_keys!(&block) ⇒ Hash
(also: #collect_keys!)
Emulates #map_keys on place.
-
#map_pairs(&block) ⇒ Hash
(also: #collect_pairs)
Returns a new hash with the results of running block once for every pair in
self
. -
#map_pairs!(&block) ⇒ Hash
(also: #collect_pairs!)
Emulates #map_pairs on place.
-
#map_values(&block) ⇒ Hash
(also: #collect_values)
Returns a new hash with the results of running block once for every value in
self
. -
#map_values!(&block) ⇒ Hash
(also: #collect_values!)
Emulates #map_values on place.
-
#recreate ⇒ Hash
Recreates the hash, so creates empty one and assigns the same default values.
-
#recreate! ⇒ Hash
Recreates the hash in place, so creates empty one, assigns the same default values and replaces the old one.
-
#remove!(&block) ⇒ Hash
Moves selected pairs outside the hash, so returns them.
-
#reverse ⇒ Hash
Reverses order of the hash pairs.
-
#reverse! ⇒ Hash
Reverses order of the hash pairs, replaces the original one.
-
#some?(&block) ⇒ Boolean
Checks, at least one element value follows condition expressed in block.
-
#sort!(&block) ⇒ Hash
Simulates sorting in place.
Class Method Details
.combine(keys, values) ⇒ Hash
Combines two arrays to Hash. First array will be keys, second one will be values.
499 500 501 502 503 504 505 506 |
# File 'lib/hash-utils/hash.rb', line 499 def self.combine(keys, values) result = { } keys.each_index do |i| result[keys[i]] = values[i] end return result end |
.create(default = nil, hash = { }, &block) ⇒ Hash
Creates hash by setting default settings in one call.
36 37 38 39 40 41 42 43 44 |
# File 'lib/hash-utils/hash.rb', line 36 def self.create(default = nil, hash = { }, &block) hash.default = default if not block.nil? hash.default_proc = block end return hash end |
.define(values = { }, default = nil, &block) ⇒ Hash
Defines hash by setting the default value or an Proc
and content.
21 22 23 24 |
# File 'lib/hash-utils/hash.rb', line 21 def self.define(values = { }, default = nil, &block) hash = self[values] self::create(default, hash, &block) end |
Instance Method Details
#all?(&block) ⇒ Boolean
(since 0.10.0, conflict with built-in method)
This method is currently in conflict with Ruby 1.9.2 Hash#all? method. Given block arity is checked, so code should be compatible for now, but this method will be probably moved around 0.13.0
for performance reasons of sure to deprecated module.
Checks, all elements values follow condition expressed in block. Block must return boolean.
If it’s empty, returns true
.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/hash-utils/hash.rb', line 261 def all?(&block) if block.arity == 2 self.all_pairs? &block end if self.empty? or block.nil? return true end self.each_value do |v| if block.call(v) == false return false end end return true end |
#all_pairs?(&block) ⇒ Boolean
Since version 0.13.0 will be replaced by an alias to Ruby built-in #all?
.
Checks, all elements follow condition expressed in block. Block must return boolean.
If it’s empty, returns true
.
292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/hash-utils/hash.rb', line 292 def all_pairs?(&block) if self.empty? return true end self.each_pair do |k, v| if block.call(k, v) == false return false end end return true end |
#asort(&block) ⇒ Hash
Sorts hash according to values. Keeps key associations. It’s equivalent of PHP asort().
Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn’t maintain pair order in Hashes.
400 401 402 403 404 405 |
# File 'lib/hash-utils/hash.rb', line 400 def asort(&block) if block.nil? block = Proc::new { |a, b| a <=> b } end Hash[self.sort { |a, b| block.call(a[1], b[1]) }] end |
#asort!(&block) ⇒ Hash
Sorts hash according to values, replaces the original one.
416 417 418 |
# File 'lib/hash-utils/hash.rb', line 416 def asort!(&block) self.replace(self.asort(&block)) end |
#compact ⇒ Hash
Returns a copy of self
with all nil
elements removed.
104 105 106 |
# File 'lib/hash-utils/hash.rb', line 104 def compact self.reject { |k, v| v.nil? } end |
#compact! ⇒ Hash
Removes nil
elements from the hash. Returns nil
if no changes were made, otherwise returns self
.
116 117 118 |
# File 'lib/hash-utils/hash.rb', line 116 def compact! self.reject! { |k, v| v.nil? } end |
#has_all?(keys) ⇒ Boolean Also known as: has_keys?
Indicates, all of the keys are available in Hash.
453 454 455 456 457 458 459 460 461 |
# File 'lib/hash-utils/hash.rb', line 453 def has_all?(keys) keys.each do |key| if not self.has_key? key return false end end return true end |
#has_some?(keys) ⇒ Boolean
Indicates, some of the keys are available in Hash.
473 474 475 476 477 478 479 480 481 |
# File 'lib/hash-utils/hash.rb', line 473 def has_some?(keys) keys.each do |key| if self.has_key? key return true end end return false end |
#keys_to_sym ⇒ Hash
Converts all keys to symbols.
228 229 230 |
# File 'lib/hash-utils/hash.rb', line 228 def keys_to_sym self.map_keys { |k| k.to_sym } end |
#keys_to_sym! ⇒ Hash
Emulates #keys_to_sym on place. In fact, replaces old hash by new one.
240 241 242 |
# File 'lib/hash-utils/hash.rb', line 240 def keys_to_sym! self.replace(self.keys_to_sym) end |
#ksort(&block) ⇒ Hash
Sorts hash according to keys. It’s equivalent of PHP ksort().
Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn’t maintain pair order in Hashes.
366 367 368 369 370 371 |
# File 'lib/hash-utils/hash.rb', line 366 def ksort(&block) if block.nil? block = Proc::new { |a, b| a <=> b } end Hash[self.sort { |a, b| block.call(a[0], b[0]) }] end |
#ksort!(&block) ⇒ Hash
Sorts hash according to keys, replaces the original one.
382 383 384 |
# File 'lib/hash-utils/hash.rb', line 382 def ksort!(&block) self.replace(self.ksort(&block)) end |
#map_keys(&block) ⇒ Hash Also known as: collect_keys
Returns a new hash with the results of running block once for every key in self
.
166 167 168 169 170 |
# File 'lib/hash-utils/hash.rb', line 166 def map_keys(&block) self.map_pairs do |k, v| [block.call(k), v] end end |
#map_keys!(&block) ⇒ Hash Also known as: collect_keys!
Emulates #map_keys on place. In fact, replaces old hash by new one.
183 184 185 |
# File 'lib/hash-utils/hash.rb', line 183 def map_keys!(&block) self.replace(self.map_keys(&block)) end |
#map_pairs(&block) ⇒ Hash Also known as: collect_pairs
Returns a new hash with the results of running block once for every pair in self
.
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/hash-utils/hash.rb', line 129 def map_pairs(&block) new = self.recreate self.each_pair do |k, v| new_k, new_v = block.call(k, v) new[new_k] = new_v end return new end |
#map_pairs!(&block) ⇒ Hash Also known as: collect_pairs!
Emulates #map_pairs on place. In fact, replaces old hash by new one.
151 152 153 |
# File 'lib/hash-utils/hash.rb', line 151 def map_pairs!(&block) self.replace(self.map_pairs(&block)) end |
#map_values(&block) ⇒ Hash Also known as: collect_values
Returns a new hash with the results of running block once for every value in self
.
198 199 200 201 202 |
# File 'lib/hash-utils/hash.rb', line 198 def map_values(&block) self.map_pairs do |k, v| [k, block.call(v)] end end |
#map_values!(&block) ⇒ Hash Also known as: collect_values!
Emulates #map_values on place. In fact, replaces old hash by new one.
215 216 217 |
# File 'lib/hash-utils/hash.rb', line 215 def map_values!(&block) self.replace(self.map_values(&block)) end |
#recreate ⇒ Hash
Recreates the hash, so creates empty one and assigns the same default values.
54 55 56 |
# File 'lib/hash-utils/hash.rb', line 54 def recreate self.class::create(self.default, &self.default_proc) end |
#recreate! ⇒ Hash
Recreates the hash in place, so creates empty one, assigns the same default values and replaces the old one.
66 67 68 |
# File 'lib/hash-utils/hash.rb', line 66 def recreate! self.replace(self.recreate) end |
#remove!(&block) ⇒ Hash
Moves selected pairs outside the hash, so returns them. Output hash has the same default settings.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/hash-utils/hash.rb', line 79 def remove!(&block) result = self.recreate delete = [ ] self.each_pair do |k, v| if block.call(k, v) result[k] = v delete << k end end delete.each do |k| self.delete(k) end return result end |
#reverse ⇒ Hash
Reverses order of the hash pairs.
Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn’t maintain pair order in Hashes.
430 431 432 |
# File 'lib/hash-utils/hash.rb', line 430 def reverse Hash[self.to_a.reverse] end |
#reverse! ⇒ Hash
Reverses order of the hash pairs, replaces the original one.
441 442 443 |
# File 'lib/hash-utils/hash.rb', line 441 def reverse! self.replace(self.reverse) end |
#some?(&block) ⇒ Boolean
Checks, at least one element value follows condition expressed in block. Block must return boolean.
315 316 317 318 319 |
# File 'lib/hash-utils/hash.rb', line 315 def some?(&block) self.one? do |pair| block.call(pair[1]) end end |