Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change hash keys in the lower case using ruby

Given hash with nested documents:

myHash = {
  "MemberId"=>"ABC0001", 
  "MemberName"=>"Alan", 
  "details"=>[
    {"LineNumber"=>"4.1", "Item"=>"A0001", "Description"=>"Apple"}, 
    {"LineNumber"=>"5.1", "Item"=>"A0002"}, 
    {"LineNumber"=>"6.1", "Item"=>"Orange"}
  ]
}

I want to change it so it will look like:

{
  "memberid"=>"ABC0001", 
  "membername"=>"Alan", 
  "details"=>[
    {"linenumber"=>"4.1", "item"=>"A0001", "description"=>"Apple"}, 
    {"linenumber"=>"5.1", "item"=>"A0002"}, 
    {"linenumber"=>"6.1", "item"=>"Orange"}
  ]
}

In other words, I want to change to lower case if any in the hash key. I understand I'll have to iterate through the hash and use downcase method. If there any easy way of doing this in ruby?

like image 917
Askar Avatar asked Jun 08 '13 12:06

Askar


People also ask

How to modify hashes in Ruby?

Modifying hashes in Ruby: Hash can be modified by adding or deleting a key value/pair in an already existing hash. Also, you can change the existing value of key in the hash.

How do I change a ruby string to uppercase or lowercase?

Ruby strings have methods to convert them to uppercase and lowercase. The method names of the methods are upcase and downcase respectively. Calling the downcase or upcase method on a string will return the lowercase or uppercase version of the string, but the original variable won't change.

How do you change the value of a hash value?

Between curly braces {}, the key/value pairs are created. Fetching hash values: To fetch a hash value always put the required key within the square bracket []. Modifying hashes in Ruby: Hash can be modified by adding or deleting a key value/pair in an already existing hash. Also, you can change the existing value of key in the hash.

What are keys and values in a hash?

It’s a nicer syntax that allows you to create hashes without the hash-rocket ( =>) symbol, which is a valid, but older way to do it. Values can be any Ruby object. Keys can also be anything, but symbols (like :banana) & strings are the most common type of keys you’ll find.


4 Answers

You can simple do

hash.transform_keys(&:downcase)

to change hash keys to lowercase.

you can refer my answer https://stackoverflow.com/a/54090178/8247133

like image 178
Touseef Murtaza Avatar answered Oct 05 '22 18:10

Touseef Murtaza


class Hash
  def downcase_key
    keys.each do |k|
      store(k.downcase, Array === (v = delete(k)) ? v.map(&:downcase_key) : v)
    end
    self
  end
end

myHash.downcase_key
like image 21
sawa Avatar answered Oct 05 '22 20:10

sawa


def f h
  Hash[h.map{|k,v| v.class == Array ? [k,v.map{|r| f r}.to_a] : [k.downcase,v]}]
end

proof

like image 28
tkroman Avatar answered Oct 05 '22 20:10

tkroman


I would first create a method on Hash that allows you to map the keys to new values:

class Hash
  def map_keys! &blk
    keys.each do |k|
      new_k = blk.call(k)
      self[new_k] = delete(k)
    end
    self
  end

  def map_keys &blk
    dup.map_keys!(&blk)
  end
end

You can now downcase the first level with

myHash.map_keys!(&:downcase)

myHash now contains:

{"details"=>
  [{"LineNumber"=>"4.1", "Item"=>"A0001", "Description"=>"Apple"},
   {"LineNumber"=>"5.1", "Item"=>"A0002"},
   {"LineNumber"=>"6.1", "Item"=>"Orange"}],
 "memberid"=>"ABC0001",
 "membername"=>"Alan"}

The nested hashes can be converted with

myHash['details'].each{|h| h.map_keys!(&:downcase) }

myHash now contains:

{"details"=>
  [{"linenumber"=>"4.1", "item"=>"A0001", "description"=>"Apple"},
   {"linenumber"=>"5.1", "item"=>"A0002"},
   {"linenumber"=>"6.1", "item"=>"Orange"}],
 "memberid"=>"ABC0001",
 "membername"=>"Alan"}
like image 27
Patrick Oscity Avatar answered Oct 05 '22 19:10

Patrick Oscity