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(r) end end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 5, 2010, at 7:36 PM, Me 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(r) > end > endwhy not use what Rails already provides via symbolize_keys! ..... http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Habgood 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(r) > end > end-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.