Method: Weak::Map#initialize
- Defined in:
- lib/weak/map.rb
#initialize(default_value = UNDEFINED, &default_proc) ⇒ Map
Returns a new empty Weak::Map object.
The initial default value and initial default proc for the new hash depend on which form above was used.
If neither an default_value nor a block is given, initializes both the
default value and the default proc to nil:
map = Weak::Map.new
map.default # => nil
map.default_proc # => nil
If a default_value is given but no block is given, initializes the
default value to the given default_value and the default proc to nil:
map = Hash.new(false)
map.default # => false
map.default_proc # => nil
If a block is given but no default_value, stores the block as the
default proc and sets the default value to nil:
map = Hash.new { |map, key| "Default value for #{key}" }
map.default # => nil
map.default_proc.class # => Proc
map[:nosuch] # => "Default value for nosuch"
If both a block and a default_value are given, raises an ArgumentError
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/weak/map.rb', line 316 def initialize(default_value = UNDEFINED, &default_proc) clear if UNDEFINED.equal?(default_value) @default_value = nil @default_proc = default_proc elsif block_given? raise ArgumentError, "wrong number of arguments (given 1, expected 0)" else @default_value = default_value @default_proc = nil end end |