Class: Hash

Inherits:
Object show all
Defined in:
lib/moon/packages/std/core_ext/hash.rb,
lib/moon/packages/micro_json/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Checks if the Hash has any content or not.

Examples:

{}.blank?       #=> true
{ a: 1 }.blank? #=> false

Returns:


50
51
52
# File 'lib/moon/packages/std/core_ext/hash.rb', line 50

def blank?
  empty?
end

#exclude(*keys) ⇒ Hash

Creates a new hash without the provided keys.

Parameters:

  • keys (Object)

    keys to exclude

Returns:


58
59
60
# File 'lib/moon/packages/std/core_ext/hash.rb', line 58

def exclude(*keys)
  reject { |key, _| keys.include?(key) }
end

#fetch_multi(*keys) ⇒ Array<Object>

Similar to #values_of, uses #fetch instead to retrieve the values.

Examples:

{ a: 1, b: 2, c: 3 }.fetch_multi(:a, :b) #=> [1, 2]

Parameters:

Returns:


39
40
41
# File 'lib/moon/packages/std/core_ext/hash.rb', line 39

def fetch_multi(*keys)
  keys.map { |key| fetch(key) }
end

#has_slice?(other) ⇒ Boolean

Has checks if target hash has all the data the other has, this is different from #eql? which checks for complete matches.

Examples:

hash = { a: 2, b: 3, c: 4 }
slice1 = { a: 2, b: 3 }
slice2 = { a: 2, c: 5 }
hash.has_slice?(slice1) #=> true
hash.has_slice?(slice2) #=> false

Parameters:

Returns:


16
17
18
19
20
21
# File 'lib/moon/packages/std/core_ext/hash.rb', line 16

def has_slice?(other)
  other.all? do |pair|
    key, value = *pair
    key?(key) && (self[key] == value)
  end
end

#permit(*keys) ⇒ Hash

Creates a new hash with only provided keys, unlike slice, the result Hash may not have all the keys specified.

Parameters:

  • keys (Object)

    keys to permit

Returns:


67
68
69
# File 'lib/moon/packages/std/core_ext/hash.rb', line 67

def permit(*keys)
  select { |key, _| keys.include?(key) }
end

#remap {|key| ... } ⇒ Object

Creates a new hash by replacing all its keys with the value from the block.

Yield Parameters:

  • key (Object)

    key to replace

Yield Returns:

  • (Object)

    new_key value to replace key with


75
76
77
# File 'lib/moon/packages/std/core_ext/hash.rb', line 75

def remap
  each_with_object({}) { |a, hash| hash[yield a[0]] = a[1] }
end

#slice(*keys) ⇒ Hash

Creates a new Hash from the provided keys, using the values from the Hash. Unlike permit, slice will create a Hash with all the keys given.

Parameters:

  • keys (Object)

    keys to copy from the Hash

Returns:

  • (Hash)

    hash slice


28
29
30
# File 'lib/moon/packages/std/core_ext/hash.rb', line 28

def slice(*keys)
  keys.each_with_object({}) { |key, hsh| hsh[key] = self[key] }
end

#stringify_keysHash<String, Object>

Remaps all keys as Strings.

Returns:


89
90
91
# File 'lib/moon/packages/std/core_ext/hash.rb', line 89

def stringify_keys
  remap { |key| key.to_s }
end

#symbolize_keysHash<Symbol, Object>

Remaps all keys as Symbols.

Returns:


82
83
84
# File 'lib/moon/packages/std/core_ext/hash.rb', line 82

def symbolize_keys
  remap { |key| key.to_sym }
end

#to_jsonString

Dumps the Hash as a JSON string.

Returns:


5
6
7
# File 'lib/moon/packages/micro_json/core_ext/hash.rb', line 5

def to_json
  JSON.dump self
end