module Nuggets::Hash::DeprocMixin

Public Instance Methods

deproc → aProc click to toggle source
deproc { |proc| ... } → anObject

Removes the default proc from hash. If a block is given, yields the proc to the block, restores the default proc afterwards and returns the block's return value. Otherwise, returns the proc.

Example:

h = Hash.new { |h, k| h[k] = [] }
h.deproc { |_h| Marshal.dump(_h) }  #=> ...dump data...
h.default_proc  #=> #<Proc:...>

NOTE: Requires Ruby >= 2.0

# File lib/nuggets/hash/deproc_mixin.rb, line 46
def deproc
  default_proc, self.default_proc = self.default_proc, nil
  return default_proc unless block_given?

  begin
    yield self
  ensure
    self.default_proc = default_proc
  end
end
deproc! → _hash_ or nil click to toggle source

Removes the default proc from hash, if present, and returns hash. Otherwise, returns nil.

Example:

h = Hash.new { |h, k| h[k] = [] }
Marshal.dump(h.deproc!)  #=> ...dump data...
h.default_proc  #=> nil

NOTE: Requires Ruby >= 2.0

# File lib/nuggets/hash/deproc_mixin.rb, line 70
def deproc!
  return unless default_proc

  self.default_proc = nil
  self
end