Class: LookupHash
- Inherits:
-
Hash
- Object
- Hash
- LookupHash
- Defined in:
- lib/lookup-hash.rb
Overview
Hash intended for using as fast lookup table for simply checking of an existency of some item. It doesn’t bring any additional performance, it’s defacto only Hash with Booleans, but it’s better write:
allowed = LookupHash[:alfa, :beta]
than:
allowed = Hash[:alfa, true, :beta, true]
Direct Known Subclasses
Class Method Summary collapse
-
.[](*args) ⇒ Hash
Creates lookup hash.
Instance Method Summary collapse
-
#<<(key) ⇒ Object
(also: #add)
Adds key to lookup hash.
-
#[]=(key, value) ⇒ Object
Element Assignment—Associates the value given by value with the key given by key.
-
#default=(value) ⇒ Object
Bans set the default value.
-
#default_proc=(value) ⇒ Object
Bans set the default block.
-
#initialize ⇒ LookupHash
constructor
Returns a new, empty hash.
-
#replace(other_hash) ⇒ Object
Replaces the contents of Hash with the contents of
other_hash
.
Constructor Details
#initialize ⇒ LookupHash
All values will be converted using hash-utils
Hash#to_b to Boolean in the LookupHash. Assigning of default value block isn’t allowed.
Returns a new, empty hash. If this hash is subsequently accessed by a key that doesn‘t correspond to a hash entry, the value returned depends on the style of new used to create the hash. In the first form, the access returns nil. If obj is specified, this single object will be used for all default values. If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block‘s responsibility to store the value in the hash if required.
58 59 60 61 |
# File 'lib/lookup-hash.rb', line 58 def initialize super(false) self.map_values! { |v| v.to_b } end |
Class Method Details
.[](*args) ⇒ Hash
Creates lookup hash. Expects key names as input. If array given, treat it as just array of keys.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lookup-hash.rb', line 26 def self.[](*args) if args.first.kind_of? Array args = args.first end new = [ ] args.each do |i| new << [i, true] end result = super(new) result.default = false return result end |
Instance Method Details
#<<(key) ⇒ Object Also known as: add
Adds key to lookup hash.
95 96 97 |
# File 'lib/lookup-hash.rb', line 95 def <<(key) self[key] = true end |
#[]=(key, value) ⇒ Object
Value will be converted using hash-utils
Hash#to_b to Boolean in the LookupHash.
Element Assignment—Associates the value given by value with the key given by key. key should not have its value changed while it is in use as a key (a String
passed as a key will be duplicated and frozen).
74 75 76 |
# File 'lib/lookup-hash.rb', line 74 def []=(key, value) super(key, value.to_b) end |
#default=(value) ⇒ Object
Bans set the default value.
105 106 |
# File 'lib/lookup-hash.rb', line 105 def default= end |
#default_proc=(value) ⇒ Object
Bans set the default block.
120 121 |
# File 'lib/lookup-hash.rb', line 120 def default_proc=(value) end |
#replace(other_hash) ⇒ Object
Value will be converted using hash-utils
Hash#to_b to Boolean in the LookupHash.
Replaces the contents of Hash with the contents of other_hash
.
86 87 88 |
# File 'lib/lookup-hash.rb', line 86 def replace(other_hash) super(other_hash.map_values { |v| v.to_b }) end |