Class: Hash
- Defined in
- lib/kyanite/hash.rb,
lib/kyanite/dictionary.rb
- Subclasses
- Optimizer
Hash Additions
- Kyanite definitions
- Kyanite tests and examples
- Usage
-
require 'kyanite/hash'
Instance Method Summary (collapse)
-
- (Hash) arrayize!(*keys)
Forces some options in a methods options hash to be an array.
-
- (Hash) compact_keys!
Deletes all key-value pairs with nil-keys in-place.
-
- (Hash) compact_values!
Deletes all key-value pairs with nil-values in-place.
-
- (Hash) delete_key(key)
Deletes the key-value pair with a given key in-place.
-
- (Hash) delete_value(value)
Deletes all key-value pairs with a given value in-place.
-
- (Array) distribution(mode = :size)
Returns the distribution of size, class or any other characteristic of the enumerated elements.
-
- (Object) first
Any value.
-
- (Object) fuzzyget(key, level = 3)
Accesses the hash with keys that do not match exactly.
-
- (Object) last
Any other value.
- - (Dictionary) to_dictionary
Instance Method Details
- (Hash) arrayize!(*keys)
Forces some options in a methods options hash to be an array. Useful if both individual values and arrays are allowed as an input option.
Example:
options = { :skip => 1, :debug => true, :test => false }
puts options
=> {:skip=>1, :debug=>true, :test=>false}
options.arrayize!(:skip, :debug)
puts options
=> {:skip=>[1], :debug=>[true], :test=>false}
69 70 71 72 73 74 75 76 77 |
# File 'lib/kyanite/hash.rb', line 69 def arrayize!(*keys) keys.each do |k| if self[k] && !self[k].respond_to?(:first) self[k] = [self[k]] end end self end |
- (Hash) compact_keys!
Deletes all key-value pairs with nil-keys in-place.
28 29 30 |
# File 'lib/kyanite/hash.rb', line 28 def compact_keys! delete_if {|key, value| key.nil? } end |
- (Hash) compact_values!
Deletes all key-value pairs with nil-values in-place.
35 36 37 |
# File 'lib/kyanite/hash.rb', line 35 def compact_values! delete_if {|key, value| value.nil? } end |
- (Hash) delete_key(key)
Deletes the key-value pair with a given key in-place. Returns the modificated hash (in contrast to Rubys delete method that returns the deleted key-value pair! ).
43 44 45 |
# File 'lib/kyanite/hash.rb', line 43 def delete_key(key) delete_if { |k,v| k == key } end |
- (Hash) delete_value(value)
Deletes all key-value pairs with a given value in-place. Returns the modificated hash (in contrast to Rubys delete method that returns the deleted key-value pair! ).
50 51 52 |
# File 'lib/kyanite/hash.rb', line 50 def delete_value(value) delete_if { |k,v| v == value } end |
- (Array) distribution(mode = :size)
Returns the distribution of size, class or any other characteristic of the enumerated elements. The keys of the hash will be ignored. See also Enumerable#distribution, examples there.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/kyanite/hash.rb', line 95 def distribution( mode = :size) verteilung = Hash.new each do | key, element | value = element.respond(mode) if verteilung.has_key?(value) verteilung[value] += 1 else verteilung[value] = 1 end # if end #each verteilung.to_a.sort end |
- (Object) first
Any value
111 112 113 114 115 |
# File 'lib/kyanite/hash.rb', line 111 def first self.each do |key, value| return value end end |
- (Object) fuzzyget(key, level = 3)
Accesses the hash with keys that do not match exactly
82 83 84 85 86 87 88 89 |
# File 'lib/kyanite/hash.rb', line 82 def fuzzyget(key, level = 3) try = self[key] return try if try || level <= 0 try = self[key.to_s.downcase2] return try if try || level <= 1 try = self[key.to_s.mysqlize] return try if try || level <= 2 end |
- (Object) last
Any other value
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/kyanite/hash.rb', line 119 def last first = nil self.each do |key, value| if first.nil? first = value next else return value end #if end #do end |
- (Dictionary) to_dictionary
61 62 63 64 65 66 67 |
# File 'lib/kyanite/dictionary.rb', line 61 def to_dictionary result = Dictionary.new self.each do | key, value | result[key] = value end result end |