Skip to content

Add to_s #42

@mperham

Description

@mperham

I've used Builder for several projects over the last few years and every time I make the same API mistake: I use #to_s to finalize the built document.

xml = Builder::XmlMarkup.new
xml.foo do
  xml.bar
end
return xml.to_s

Only to find that I've injected an extra, unwanted <to_s/> element. A naive approach might try this:

class Builder::XmlMarkup
  def to_s
    @target.to_s
  end
end

But I know you just require the :<< method, thus allowing direct IO, which can't convert to a String. Perhaps something like this:

class Builder::XmlMarkup
  def to_s
    raise ArgumentError, "Cannot coerce #{@target.class.name} to String" unless @target.is_a?(String)
    @target
  end
end

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions