Skip to content

Conversation

@willhbr
Copy link

@willhbr willhbr commented Feb 2, 2026

For example if you want to write directly to a TCPSocket or some other IO.

@willhbr
Copy link
Author

willhbr commented Feb 3, 2026

Actually this doesn't really work, as HTML::Builder#build will always call .to_s on the IO so this is pointless.

I think the way to keep this flexible is to change the API a little:

  • Keep HTML.build as the main string-returning entry point
  • Make HTML::Builder.new always take an IO argument
  • Create the String::Builder in HTML::Builder.build, like this:
def self.build : String
  String.build do |io|
    new(io).build do |builder|
      with builder yield builder
    end
  end
end

@ysbaddaden does that sound reasonable? Otherwise I'll just patch this in my project to get it working how I need

@ysbaddaden
Copy link

That sounds reasonable, the buffer shall be a general IO and be created by the .build constructor, not by #initialize.

We can keep backward compatibility by deprecating the #initialize (no args) and #build methods, and adding:

  • .build(IO) overload
  • #initialize(io : IO)
  • protected #build_impl (that don't call #to_s)

The previous version would always call .to_s on the IO, which would
produce incorrect results or at the very least remove any performance
gains.

This deprecates some methods to make HTML.build the recommended
entrypoint.
@willhbr
Copy link
Author

willhbr commented Feb 3, 2026

Updated, let me know if that's not what you were thinking

Comment on lines +22 to +26
String.build do |io|
new(io).build_impl do |builder|
with builder yield builder
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polish:

Suggested change
String.build do |io|
new(io).build_impl do |builder|
with builder yield builder
end
end
String.build { |io| build(io) }

Comment on lines +50 to +52
build_impl do
with self yield self
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polish: no need to call #build_impl

Suggested change
build_impl do
with self yield self
end
with self yield self

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

Successfully merging this pull request may close these issues.

2 participants