Module: Nuggets::Hash::DeprocMixin
- Included in:
- Hash
- Defined in:
- lib/nuggets/hash/deproc_mixin.rb
Instance Method Summary collapse
-
#deproc ⇒ Object
call-seq: hash.deproc -> aProc hash.deproc { |proc| … } -> anObject.
-
#deproc! ⇒ Object
call-seq: hash.deproc! -> hash or nil.
Instance Method Details
#deproc ⇒ Object
call-seq:
hash.deproc -> aProc
hash.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
46 47 48 49 50 51 52 53 54 55 |
# 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! ⇒ Object
call-seq:
hash.deproc! -> _hash_ or nil
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
70 71 72 73 74 75 |
# File 'lib/nuggets/hash/deproc_mixin.rb', line 70 def deproc! return unless default_proc self.default_proc = nil self end |