Rubyでハッシュのキーをシンボルに変換


# ハッシュのキーを再帰的に全てシンボルに変換
def key_to_hash hash
	hash.inject({}) do |r,entry|
		if entry[1].instance_of? Hash
			entry[1] = key_to_hash(entry[1])
		end
		r.store( entry[0].to_sym, entry[1])
		r
  end
end

a = { 'hoge'=>'piyo', :foo => { 'bar' => 'BAR' } }
p key_to_hash(a)
#=> {:hoge=>"piyo", :foo=>{:bar=>"BAR"}}
Filed under: 未分類 — hikaru 9:40 PM  Comments (2)

Windows版RubyGemsのアップデート

WindowsでRails2.2.2環境を導入際にGemの1.3.1以上を入れる必要がありますが、updateには特殊な手順が必要のようだったのでメモメモ。
(続きを読む…)

Filed under: コンピュータ,プログラム — hikaru 6:42 PM  Comments (0)