Class: Hash

Inherits:
Object show all
Defined in:
lib/reactive_support/core_ext/object/blank.rb,
lib/extensions/hash_extensions.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}



54
55
56
57
58
# File 'lib/reactive_support/core_ext/object/deep_dup.rb', line 54

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)


105
106
107
# File 'lib/reactive_support/core_ext/object/blank.rb', line 105

def present?
  !blank?
end

#stringify_keysObject

The #stringify_keys method returns a hash identical to the calling hash, but with symbol keys turned into strings. It is non-destructive; the original hash is still available after it is called.

Although this method was formerly a part of ActiveSupport, it was already deprected by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { :foo => 'bar' }
dup = orig.stringify_keys

orig      #=> { :foo => 'bar' }
dup       #=> { 'foo' => 'bar' }


61
62
63
64
65
# File 'lib/extensions/hash_extensions.rb', line 61

def stringify_keys
  dup = {}
  self.each {|k, v| dup[k.to_s] = v }
  dup
end

#stringify_keys!Object

The #stringify_keys! method converts symbol hash keys into strings. It is a destructive method; the original hash is changed when this method is called.

Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { :foo => 'bar' }
orig.symbolize_keys!

orig      #=> { 'foo' => 'bar' }


81
82
83
84
85
86
87
# File 'lib/extensions/hash_extensions.rb', line 81

def stringify_keys!
  keys.each do |key|
    self[(key.to_s rescue key) || key] = delete(key)
  end

  self
end

#symbolize_keysObject

The #symbolize_keys method returns a hash identical to the calling hash but with string keys turned into symbols. It is non-destructive; the original hash is still available after it is called.

Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { 'foo' => 'bar' }
dup = orig.symbolize_keys

orig      #=> { 'foo' => 'bar' }
dup       #=> { :foo => 'bar' }


18
19
20
21
22
# File 'lib/extensions/hash_extensions.rb', line 18

def symbolize_keys
  dup = {}
  self.each {|k, v| dup[k.to_sym] = v }
  dup
end

#symbolize_keys!Object

The #symbolize_keys! method converts string hash keys into symbols. It is a destructive method; the original hash is changed when this method is called.

Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { 'foo' => 'bar' }
orig.symbolize_keys!

orig      #=> { :foo => 'bar' }


38
39
40
41
42
43
44
# File 'lib/extensions/hash_extensions.rb', line 38

def symbolize_keys!
  keys.each do |key|
    self[(key.to_sym rescue key) || key] = delete(key)
  end

  self
end