class LookupHash
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]
Public Class Methods
Creates lookup hash. Expects key names as input. If array given, treat it as just array of keys.
@return [Hash] new hash
# 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
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.
@note All values will be converted using hash-utils
Hash#to_b
to Boolean in the {LookupHash}. Assigning of default value block isn't allowed.
@see ruby-doc.org/core/classes/Hash.html#M000718
# File lib/lookup-hash.rb, line 58 def initialize super(false) self.map_values! { |v| v.to_b } end
Public Instance Methods
Adds key to lookup hash. @param [Object] key key for add
# File lib/lookup-hash.rb, line 95 def <<(key) self[key] = true end
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).
@note Value will be converted using hash-utils
Hash#to_b
to Boolean in the {LookupHash}.
@see ruby-doc.org/core/classes/Hash.html#M000729
# File lib/lookup-hash.rb, line 74 def []=(key, value) super(key, value.to_b) end
Bans set the default value.
# File lib/lookup-hash.rb, line 105 def default= end
Bans set the default block.
# File lib/lookup-hash.rb, line 120 def default_proc=(value) end
Replaces the contents of Hash with the contents of other_hash
.
@note Value will be converted using hash-utils
Hash#to_b
to Boolean in the {LookupHash}.
@see ruby-doc.org/core/classes/Hash.html#M000757
# File lib/lookup-hash.rb, line 86 def replace(other_hash) super(other_hash.map_values { |v| v.to_b }) end