-2

I need to change the keys of a Hash, I am currently given the key as a String and I need it to be the normal key of a hash. I leave the example below.

I have tried some methods that I found as Symbolize_keys, but they have not worked, says that the method does not exist and I use it as it is in Ruby's Apidock.

i have this:

hash = {"name"=>"Jhon","lastname"=>"Doe"}

and i need to convert This:

hash = {:name=>"Jhon",:lastname=>"Doe"}

Thanks Friends

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
Miguel Abdón
  • 31
  • 1
  • 5

1 Answers1

7

If you have Ruby 2.5:

hash.transform_keys!(&:to_sym)
Mark Thomas
  • 37,131
  • 11
  • 74
  • 101