Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | Hash#to_xml

概要

Hash#to_xml

詳細

Hash#to_xml について

Hash#to_xml

Hashをxmlに変換する。

サンプル

# encoding: utf-8
require 'active_support/core_ext/hash/conversions'
require 'tbpgr_utils'

I18n.enforce_available_locales = true
hash = {
  :key1 => :value1,
  :key2 => :value2,
  :key3 => :value3,
}
bulk_puts_eval binding, <<-EOS
hash.to_xml
hash.to_xml
hash.to_xml
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

hash.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <key1 type=\"symbol\">value1</key1>\n  <key2 type=\"symbol\">value2</key2>\n  <key3 type=\"symbol\">value3</key3>\n</hash>\n"
hash.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <key1 type=\"symbol\">value1</key1>\n  <key2 type=\"symbol\">value2</key2>\n  <key3 type=\"symbol\">value3</key3>\n</hash>\n"
hash.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <key1 type=\"symbol\">value1</key1>\n  <key2 type=\"symbol\">value2</key2>\n  <key3 type=\"symbol\">value3</key3>\n</hash>\n"