Module: Fountain::Util

Defined in:
lib/fountain/util.rb

Overview

Fountain generic utility functions

Class Method Summary collapse

Class Method Details

.slice_hash(hash, *keys) ⇒ Object

Slice keys from hash

Parameters:

  • hash (Hash)

    A hash to slice key/value pairs from

  • *keys (Array)

    The keys to be sliced

Returns:

  • Hash filtered by keys



34
35
36
37
38
39
40
41
42
# File 'lib/fountain/util.rb', line 34

def slice_hash(hash, *keys)
  return {} if keys.empty?

  new_hash = {}
  hash.each do |key, value|
    new_hash[key] = value if keys.include? key
  end
  new_hash
end

.stringify_hash_keys(hash) ⇒ Object

Stringify symbolized hash keys

Parameters:

  • hash (Hash)

    A string/symbol keyed hash

Returns:

  • Stringified hash



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fountain/util.rb', line 15

def stringify_hash_keys(hash)
  new_hash = {}
  hash.each do |key, value|
    new_hash[key.to_s] =
      if value.is_a? Hash
        stringify_hash_keys value
      else
        value
      end
  end
  new_hash
end