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
46 def deproc
47   default_proc, self.default_proc = self.default_proc, nil
48   return default_proc unless block_given?
49 
50   begin
51     yield self
52   ensure
53     self.default_proc = default_proc
54   end
55 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
70 def deproc!
71   return unless default_proc
72 
73   self.default_proc = nil
74   self
75 end