Class: HashKit::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_kit/helper.rb

Instance Method Summary collapse

Instance Method Details

#stringify(hash) ⇒ Object

This method is called to convert all the keys of a hash into strings to allow consistent usage of hashes within your Ruby application.



12
13
14
15
16
# File 'lib/hash_kit/helper.rb', line 12

def stringify(hash)
  {}.tap do |h|
    hash.each { |key, value| h[key.to_s] = map_value_string(value) }
  end
end

#symbolize(hash) ⇒ Object

This method is called to convert all the keys of a hash into symbols to allow consistent usage of hashes within your Ruby application.



5
6
7
8
9
# File 'lib/hash_kit/helper.rb', line 5

def symbolize(hash)
  {}.tap do |h|
    hash.each { |key, value| h[key.to_sym] = map_value_symbol(value) }
  end
end

#to_hash(obj) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/hash_kit/helper.rb', line 18

def to_hash(obj)
  hash = {}
  obj.instance_variables.each do |key|
    hash[key[1..-1].to_sym] = deeply_to_hash(obj.instance_variable_get(key))
  end
  hash
end