Ruby2html is a Ruby gem that converts pure Ruby code into HTML. It features C extension optimizations for high-performance HTML rendering, escaping, and attribute generation. The project is both a standalone gem and a Rails demo application.
Performance Philosophy: Prioritize speed over memory usage. Modern systems have abundant memory, so optimize for execution speed. The C extension implements fast string operations with pre-allocated buffers and direct memory manipulation.
bin/setup # Install dependencies, prepare database, compile C extension
bundle install # Install gem dependencies
rake compile # Compile the C extension (ext/ruby2html/ruby2html.c)bundle exec rspec # Run all tests
bundle exec rspec spec/features/ # Run feature tests
bundle exec rspec spec/gem/ # Run gem unit tests
bundle exec rspec spec/benchmarks/ # Run benchmark testsbin/rails server # Start Rails server (demo app)
bin/rails console # Rails consolerake build # Runs rake compile first, then builds gem
gem build ruby2html.gemspecThe codebase operates in two modes:
- Gem mode: Core library in
lib/gem/ruby2html/- standalone rendering engine - Rails demo mode: Full Rails app in
app/,config/, demonstrating gem usage
Located in ext/ruby2html/ruby2html.c:
fast_escape_html: Two-pass HTML escaping (pre-calculate size, then escape)fast_attributes_to_s: Direct string buffer manipulation for attribute generationfast_buffer_append: Optimized string concatenation
These C methods are called by lib/gem/ruby2html/render.rb for performance-critical operations.
lib/gem/ruby2html/render.rb - Main renderer with metaprogramming for HTML tag generation:
- Pre-generates methods for all HTML5 tags at class load time via
class_eval(METHOD_DEFINITIONS) - Uses
String.new(capacity: N)for pre-allocated buffers to minimize allocations - Dynamically delegates to Rails helpers via
method_missingwhen context responds - Thread-safe via
Thread.current[:__ruby2html_renderer__]for nested components
Key methods:
initialize(context, &block): Sets up renderer with 4KB pre-allocated output bufferrender: Executes root block, returns HTML string (safe buffer in Rails)plain(text): Outputs raw HTML or escaped text- Tag methods:
div,h1,p, etc. - dynamically generated
lib/gem/ruby2html/railtie.rb:
- Registers
.rbtemplate handler viaActionView::Template.register_template_handler - Ignores
*.html.rbfiles from Zeitwerk autoloading - Templates are executed in context of view with access to instance variables
lib/gem/ruby2html/rails_helper.rb:
html(context, &block)helper for use in ERB templates- Include in controllers to access in views
lib/gem/ruby2html/rails_components/:
- Wrappers for Rails helpers:
link_to,image_tag,form_with,button_to - These integrate Rails view helpers with Ruby2html's rendering pipeline
lib/gem/ruby2html/component_helper.rb:
- Provides
html(&block)method for components - Thread-local renderer storage for nested component rendering
- Method missing delegation to current renderer
Components extend ApplicationComponent which includes Ruby2html::ComponentHelper.
Files ending in .html.rb are treated as Ruby2html templates:
app/views/home/index.html.rb- View templateapp/components/first_component.html.rb- Component template
Components can also define call method with html do ... end block instead of template file.
When writing or modifying code:
- String allocation: Use
String.new(capacity: N)with estimated sizes to avoid reallocation - Buffer reuse: The
@current_outputbuffer is reused for nested content generation - C extension fallback: All C methods have Ruby fallbacks if extension fails to load
- Method caching: Tag methods are pre-generated at class load, not defined dynamically per-call
- Minimal object creation: Prefer string concatenation over array joins or template interpolation
ext/ruby2html/ruby2html.c- C extension for performance-critical operationslib/gem/ruby2html/render.rb- Core rendering engine with tag generationlib/gem/ruby2html/railtie.rb- Rails template handler registrationlib/gem/ruby2html/component_helper.rb- ViewComponent integrationapp/components/application_component.rb- Base component class for demospec/benchmarks/requests_spec.rb- Performance comparison benchmarks
The demo app includes benchmarks comparing Ruby2html to ERB, Slim, and Phlex in spec/benchmarks/requests_spec.rb. Run via bundle exec rspec spec/benchmarks/.
Optional middleware Ruby2html::HtmlBeautifierMiddleware formats HTML output for development/testing. Enable in config/environments/development.rb.