Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/enumerate_hash_values/core_extensions/hash.rb
Overview
:nodoc:
Class Method Summary collapse
Instance Method Summary collapse
-
#copy ⇒ Object
Shortcut to perform a shallow copy of the hash to a new instance.
-
#copy_traits!(hash) ⇒ Object
Copy any traits (such as defaults) from the specified hash.
-
#inject_into_empty(hash_class = self.class, options = {}) ⇒ Object
Shortcut to inject into an empty hash.
Class Method Details
.new_empty(options = {}) ⇒ Object
85 86 87 88 89 |
# File 'lib/enumerate_hash_values/core_extensions/hash.rb', line 85 def new_empty( = {}) new_hash = allocate new_hash.copy_traits!([:traits_from]) if [:traits_from] new_hash end |
Instance Method Details
#copy ⇒ Object
Shortcut to perform a shallow copy of the hash to a new instance.
51 52 53 |
# File 'lib/enumerate_hash_values/core_extensions/hash.rb', line 51 def copy collect_values { |value| value } end |
#copy_traits!(hash) ⇒ Object
Copy any traits (such as defaults) from the specified hash.
56 57 58 59 60 61 62 63 64 |
# File 'lib/enumerate_hash_values/core_extensions/hash.rb', line 56 def copy_traits!(hash) self.default = hash.default # Unfortunately, we can only transfer Hash#default_proc in Ruby 1.9 because # Hash#default_proc= is only defined in Ruby 1.9. self.default_proc = hash.default_proc if respond_to?(:default_proc=) self end |
#inject_into_empty(hash_class = self.class, options = {}) ⇒ Object
Shortcut to inject into an empty hash.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/enumerate_hash_values/core_extensions/hash.rb', line 67 def inject_into_empty(hash_class = self.class, = {}) raise ArgumentError.new("#{hash_class} is not a Hash") unless hash_class.ancestors.include?(Hash) # If not explicitly specified, copy traits into the new hash if it is of # the same class. copy_traits = .has_key?(:copy_traits) ? [:copy_traits] : (hash_class == self.class) = {} [:traits_from] = self if copy_traits new_hash = hash_class.new_empty() inject(new_hash) do |hash, key_value_pair| yield hash, key_value_pair end end |