|
|||||
|
対象: XMLからHashを生成するactivesupportをインストールするとHash.from_xmlが使えるようになる。from_xmlメソッドはXMLからHashを生成してくれる便利なメソッドだ。
require "rubygems"
#require "active_support" # ActiveSupport 2.x
require "active_support/core_ext" # ActiveSupport 3.x
require "pp"
h = Hash.from_xml("<langs><lang>Japanese</lang><lang>English</lang></langs>")
pp h
実行結果は、
{"langs"=>{"lang"=>["Japanese", "English"]}}
のようになる。 尚、Hash.from_xmlメソッドはActiveSupport 3.xだとactive_support/core_extに移動になっているのでバージョンによって使い分ける必要がある。以下のようにNoMethodErrorに遭遇したときはActiveSupportのバージョンを確認してみよう。 NoMethodError: undefined method `from_xml' for Hash:Class (2010/02/21)
Copyright(C) 2004-2014 モバイル開発系(K) All rights reserved.
[Home]
|
|||||