From a268a9ada0a7554e8bbd54a09d18baf8707bde60 Mon Sep 17 00:00:00 2001 From: Eike Send Date: Tue, 24 Feb 2026 17:03:18 +0100 Subject: [PATCH 1/2] Improve documentation of BasicObject inheritance and XmlBase#nil? --- lib/builder/xmlbase.rb | 22 +++++++++++++--------- lib/builder/xmlmarkup.rb | 8 ++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/builder/xmlbase.rb b/lib/builder/xmlbase.rb index 2cddbec..14b7c69 100644 --- a/lib/builder/xmlbase.rb +++ b/lib/builder/xmlbase.rb @@ -6,8 +6,17 @@ module Builder # Generic error for builder class IllegalBlockError < RuntimeError; end - # XmlBase is a base class for building XML builders. See + # XmlBase is a base class for building XML builders. See # Builder::XmlMarkup and Builder::XmlEvents for examples. + # + # Please note, that the XmlBase class inherits from +BasicObject+, + # not from the more common +Object+. The advantage is that there are less + # instance methods which can conflict with xml element names which + # might be used. The disadvantage is that these +Object+ instance + # methods might be expected to be present by testing tools + # or IRB or other consoles for inspection. + # Prominent missing methods are: +class+, +display+, +hash+, +inspect+, + # +methods+, +object_id+, +send+ (you can use +__send__+ though). class XmlBase < BasicObject class << self @@ -16,8 +25,6 @@ class << self # Create an XML markup builder. # - # out :: Object receiving the markup. +out+ must respond to - # <<. # indent :: Number of spaces used for indentation (0 implies no # indentation and no line breaks). # initial :: Level of initial indentation. @@ -117,12 +124,9 @@ def <<(text) _text(text) end - # For some reason, nil? is sent to the XmlMarkup object. If nil? - # is not defined and method_missing is invoked, some strange kind - # of recursion happens. Since nil? won't ever be an XML tag, it - # is pretty safe to define it here. (Note: this is an example of - # cargo cult programming, - # cf. http://fishbowl.pastiche.org/2004/10/13/cargo_cult_programming). + # +nil?+ is an instance method not implemented on the +BasicObject+, + # but since +nil?+ won't ever be an XML tag, it is pretty safe to + # define it here. def nil? false end diff --git a/lib/builder/xmlmarkup.rb b/lib/builder/xmlmarkup.rb index cccf8d0..432b2bd 100644 --- a/lib/builder/xmlmarkup.rb +++ b/lib/builder/xmlmarkup.rb @@ -16,9 +16,9 @@ module Builder - # Create XML markup easily. All (well, almost all) methods sent to - # an XmlMarkup object will be translated to the equivalent XML - # markup. Any method with a block will be treated as an XML markup + # Create XML markup easily. All methods (except BasicObject instance methods) + # sent to an XmlMarkup object will be translated to the equivalent XML + # markup. Any method with a block will be treated as an XML markup # tag with nested markup in the block. # # Examples will demonstrate this easier than words. In the @@ -57,7 +57,7 @@ module Builder # # xm.div { #
# xm.text! "line"; xm.br # line
- # xm.text! "another line"; xmbr # another line
+ # xm.text! "another line"; xm.br # another line
# } #
# # * The special XML characters <, >, and & are converted to <, From b8e17354ae598be7defe5455c70f89af3b3b1797 Mon Sep 17 00:00:00 2001 From: Eike Send Date: Tue, 24 Feb 2026 18:25:44 +0100 Subject: [PATCH 2/2] Add tests showing that Object method names can be used as XML elements --- test/test_markupbuilder.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_markupbuilder.rb b/test/test_markupbuilder.rb index 9e0b432..c105e3f 100644 --- a/test/test_markupbuilder.rb +++ b/test/test_markupbuilder.rb @@ -122,6 +122,14 @@ def test_complex assert_equal %{T}, @xml.target! end + def test_object_method_names + @xml.class do + @xml.methods('x') + @xml.object_id('1') + end + assert_equal %{x1}, @xml.target! + end + def test_funky_symbol @xml.tag!("non-ruby-token", :id=>1) { |x| x.ok } assert_equal %{}, @xml.target!