Class: Hash

Inherits:
Object show all
Defined in:
lib/reactive_support/core_ext/object/blank.rb,
lib/reactive_support/core_ext/object/deep_dup.rb

Overview

Ruby’s core Hash class. See documentation for version 2.1.3, 2.0.0, or 1.9.3.

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

When called on a hash, the #deep_dup method duplicates all the key-value pairs in the hash recursively, so that actions on the duplicate do not affect the original hash:

hash = {b: 2} # => {b: 2} dup, deep = hash.dup, hash.deep_dup # => {b: 2}, {b: 2}

deep[:b] = 14 # => {b: 14} p hash # => {b: 2}

dup[:b] = 14 # => {b: 14} p hash # => {b: 14}



49
50
51
52
53
# File 'lib/reactive_support/core_ext/object/deep_dup.rb', line 49

def deep_dup
  self.each_with_object(dup) do |(key, value), hash|
    hash[key.deep_dup] = value.deep_dup
  end
end

#present?Boolean

When called on a hash (or any enumerable), the #present? method returns true if the hash is not empty, even if its elements have blank values:

{ foo: :bar }.present?      # => true
{ 'bar' => nil }.present?   # => true
{}.present?                 # => false

Returns:

  • (Boolean)


82
83
84
# File 'lib/reactive_support/core_ext/object/blank.rb', line 82

def present?
  !blank?
end