Skip to content

Commit 7773c32

Browse files
apotonickdrogus
authored andcommitted
corrected docs for Hash#to_xml and added examples.
1 parent 54f3aa8 commit 7773c32

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

activesupport/lib/active_support/core_ext/hash/conversions.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,22 @@ class Hash
2626
#
2727
# * If +value+ is a callable object it must expect one or two arguments. Depending
2828
# on the arity, the callable is invoked with the +options+ hash as first argument
29-
# with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. Its
30-
# return value becomes a new node.
29+
# with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. The
30+
# callable can add nodes by using <tt>options[:builder]</tt>.
31+
#
32+
# "foo".to_xml(lambda { |options, key| options[:builder].b(key) })
33+
# # => "<b>foo</b>"
3134
#
3235
# * If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
36+
#
37+
# class Foo
38+
# def to_xml(options)
39+
# options[:builder].bar "fooing!"
40+
# end
41+
# end
42+
#
43+
# {:foo => Foo.new}.to_xml(:skip_instruct => true)
44+
# # => "<hash><bar>fooing!</bar></hash>"
3345
#
3446
# * Otherwise, a node with +key+ as tag is created with a string representation of
3547
# +value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added.

0 commit comments

Comments
 (0)