I’m trying out Builder::XMLMarkup to build some xml and it keeps adding an empty element to my xml.
Why does it do this and how do I stop it?
xml = Builder::XmlMarkup.new
=> <inspect/>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Builder implements a version of
method_missingthat adds tags given by the name of the method call.Assuming you are playing in irb (or rails’ console), irb’s default behaviour when you evaluate an expression (such as
Builder::XmlMarkup.new) is to callinspecton it, in order to generate a string to show to you. In the case of builder,inspectisn’t the usual rubyinspectmethod – it falls through tomethod_missingand adds the tag.This will only happen when playing with ruby interactively. You can do stuff like
Here the result of the expression is
falseso irb callsinspecton that and leaves your builder object alone.It can be awkward to keep doing this continually. If you do
then
xmlwill still be a builder object that display its content when inspected by irb. You won’t be able to create tags calledinspect(other than by usingtag!) but that is usually a minor inconvenience.