Rails symbolize_keys å帰ç
Railsã«ã¯Hash#symbolize_keysã¨ããã¡ã½ãããç¨æããã¦ãããHashã®ãã¼ãæååããã·ã³ãã«ã«ãã¦ãããã®ã§ãããã§å®å¿ã ã¼ãªãã¦æã£ã¦ãã¨ãããã·ã¥ã®ä¸èº«ãã·ã³ãã«ã«ãªããªãã£ãããããå ·ä½çã«ã¯
{ 'key': 1 'hash': { 'key': 2 } }.symbolize_keys # => { key: 1 hash: { 'key': 2 } }
ã¿ãããªäºã«ãªãããããå帰çã«ãããã®ã§ãªãã¨ããããã¨ããæã¯
class Hash def symbolize_keys! keys.each do |key| case (v = delete(key)) when Hash v = v.symbolize_keys when Array v = v.map{|x| (x.symbolize_keys rescue x) } end self[(key.to_sym rescue key) || key] = v end self end end
ããã§ãªãã¨ããªããé åã®ä¸ã®Hashä¸ã®ãã¼ãã¡ããã¨ã·ã³ãã«ã«ãªããYAMLã«ã³ã³ãã£ã°æ¸ãã¦ãæã¨ãã«è©°ã¾ãã®ã§è¦æ³¨æã