Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_s #42

Open
mperham opened this issue May 6, 2013 · 6 comments
Open

Add to_s #42

mperham opened this issue May 6, 2013 · 6 comments

Comments

@mperham
Copy link

mperham commented May 6, 2013

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?

@jimweirich
Copy link
Owner

I try to avoid all normal method names on the MarkUp object to avoid clashes with XML tag names. Will someone have an XML tag named "to_s"? Probably not, but I'm not in fond of setting a precedent.

@mperham
Copy link
Author

mperham commented May 31, 2013

What about alias_method :to_s!, :target!? At least that will trigger recognition in the mind of the developer.

@mperham
Copy link
Author

mperham commented May 31, 2013

Or maybe not an alias but the method I gave above.

@jimweirich
Copy link
Owner

I'm ok with the to_s! method, (provided it fails for non-string targets). But I'm not seeing how it actually solves the problem of forgetting that to_s doesn't actually exist.

@mperham
Copy link
Author

mperham commented Jun 1, 2013

Because it jogs the memory when reading the documentation. When looking through the methods on the object, target! looks like another XML declaration whereas to_s! would make me investigate that method because that's really what the developer is looking for: "how do I convert this thing to a String?"

@jc00ke
Copy link

jc00ke commented May 7, 2015

I think this causes an issue with pry and irb too, unless I'm misunderstanding it. Builder::XmlMarkup#inspect is being treated like a tag, which makes it hard to explore in a console.

irb(main):024:0> builder = Builder::XmlMarkup.new
=> <inspect/>
irb(main):025:0> builder
=> <inspect/><inspect/>
irb(main):026:0> builder
=> <inspect/><inspect/><inspect/>

It seems like a few methods, #to_s, #inspect and #object_id for example, should not modify the instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants