Palak Chaudhary
Palak Chaudhary

Reputation: 191

Is it possible to convert a symbol to string of key in rails

Changing a symbol to string in ruby

The following works

a = :'title of the book'

a = a.to_s

But while trying to change the key of hash, the above does not work.

Upvotes: 0

Views: 2235

Answers (2)

chen
chen

Reputation: 11

In this case, I suggest: Hash.stringify_keys This would convert {:Key => value} to {"Key" => value}

Upvotes: 1

Ecnalyr
Ecnalyr

Reputation: 5802

You can use stringify_keys to change all of your hash's keys into strings.

Upvotes: 2

Related Questions