Method: Weak::Map#default_proc=

Defined in:
lib/weak/map.rb

#default_proc=(proc) ⇒ Proc?

Sets the default proc for self to proc and clears the #default value.

Parameters:

  • proc (Proc, #to_proc nil)

    a Proc which can be called with two arguments: the map and the rquested non-exiting key. The proc is expected to return the default value for the key. Whe giving nil, the default proc is cleared.

Returns:

  • (Proc, nil)

    the new default proc

Raises:

  • (TypeError)

    if the given proc can not be converted to a Proc.



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/weak/map.rb', line 407

def default_proc=(proc)
  @default_value = nil
  return @default_proc = nil if proc.nil?

  if Proc === proc
    default_proc = proc
  elsif proc.respond_to?(:to_proc)
    default_proc = proc.to_proc
    unless Proc === default_proc
      raise TypeError, "can't convert #{proc.class} to Proc " \
        "(#{proc.class}#to_proc gives #{default_proc.class})"
    end
  else
    raise TypeError, "no implicit conversion of #{proc.class} into Proc"
  end

  if default_proc.lambda?
    arity = default_proc.arity
    if arity != 2 && (arity >= 0 || arity < -3)
      arity = -arity - 1 if arity < 0
      raise TypeError, "default_proc takes two arguments (2 for #{arity})"
    end
  end
  @default_proc = default_proc

  proc
end