# Hash string to symbol

**URL:** <https://www.ruby-forum.com/t/hash-string-to-symbol/186382>\
**Category:** Rails\
**Created:** [April 6, 2010, 4:38am UTC](https://www.ruby-forum.com/t/hash-string-to-symbol/186382 "2010-04-06T04:38:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 6, 2010, 4:38am UTC](https://www.ruby-forum.com/t/hash-string-to-symbol/186382/1 "2010-04-06T04:38:02Z")

</div>

I found a method to convert the keys that are string to symbols. I  
included the file in the lib directory but when I call it it says it  
does not recognize the method. Ideas?

class Hash

# Recursively replace key names that should be symbols with symbols.

def key\_strings\_to\_symbols!  
r = Hash.new  
self.each\_pair do |k,v|  
if ((k.kind\_of? String) and k =~ /^:/)  
v.key\_strings\_to\_symbols! if v.kind\_of? Hash and  
v.respond\_to? :key\_strings\_to\_symbols!  
r[k.slice(1…-1).to\_sym] = v  
else  
v.key\_strings\_to\_symbols! if v.kind\_of? Hash and  
v.respond\_to? :key\_strings\_to\_symbols!  
r[k] = v  
end  
end  
self.replace®  
end  
end

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 6, 2010, 6:09am UTC](https://www.ruby-forum.com/t/hash-string-to-symbol/186382/2 "2010-04-06T06:09:56Z")

</div>

On Apr 5, 2010, at 7:36 PM, Me wrote:

> ```
> v.key_strings_to_symbols! if v.kind_of? Hash and
> 
> ```
> 
> end  
> why not use what Rails already provides via symbolize\_keys! …

[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html)

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [April 6, 2010, 7:45am UTC](https://www.ruby-forum.com/t/hash-string-to-symbol/186382/3 "2010-04-06T07:45:56Z")

</div>

Have you included the lib file in your class like  
include Hash or  
require ‘hash.rb’

If include is present then call the method like  
object.key\_strings\_to\_symbols!

If require is present then call the method like  
class\_name = Student  
class\_name.key\_strings\_to\_symbols!

Keep rocking!

Chris H. wrote:

> I found a method to convert the keys that are string to symbols. I  
> included the file in the lib directory but when I call it it says it  
> does not recognize the method. Ideas?
> 
> class Hash
> 
> # Recursively replace key names that should be symbols with symbols.
> 
> def key\_strings\_to\_symbols!  
> r = Hash.new  
> self.each\_pair do |k,v|  
> if ((k.kind\_of? String) and k =~ /^:/)  
> v.key\_strings\_to\_symbols! if v.kind\_of? Hash and  
> v.respond\_to? :key\_strings\_to\_symbols!  
> r[k.slice(1…-1).to\_sym] = v  
> else  
> v.key\_strings\_to\_symbols! if v.kind\_of? Hash and  
> v.respond\_to? :key\_strings\_to\_symbols!  
> r[k] = v  
> end  
> end  
> self.replace®  
> end  
> end
