Class: Frozen::LookupHash

Inherits:
LookupHash show all
Defined in:
lib/lookup-hash/frozen.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 to write:

class Foo
    CONST = Frozen::LookupHash[:alfa, :beta]
end

than:

class Foo
    CONST = Hash[:alfa, true, :beta, true].freeze
end

Implicitly frozen for use in class constants.

Since:

  • 0.2.0

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LookupHash

#<<, #[]=, #default=, #default_proc=, #replace

Constructor Details

#initializeLookupHash

Note:

All values will be converted using hash-utils Hash#to_b to Boolean in the Frozen::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.



62
63
64
65
# File 'lib/lookup-hash/frozen.rb', line 62

def initialize
    super
    self.freeze
end

Class Method Details

.[](*args) ⇒ Hash

Creates lookup hash. Expects key names as input. If array given, treat it as just array of keys.

Returns:

  • (Hash)

    new hash

Since:

  • 0.2.0



41
42
43
44
# File 'lib/lookup-hash/frozen.rb', line 41

def self.[](*args)
    result = super(*args)
    result.freeze
end