Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 14, 2025 18:51
Show Gist options
  • Save havenwood/70761da84d19fd92b0f7452aa4f722d4 to your computer and use it in GitHub Desktop.
Save havenwood/70761da84d19fd92b0f7452aa4f722d4 to your computer and use it in GitHub Desktop.
My suggestion for a lambda-like strict arity alternative to a block
module Papercraft
class Block
def initialize(&) = singleton_class.define_method(:call, &)
def apply(...) = self.class.new { call(...) }
class HTML < Block
def render(...) = call(...)
def to_html = render.to_s
end
end
def self.html(&) = Block::HTML.new(&)
end
hello_world = Papercraft.html { |name| "<h1>Hello, #{name}!</h1>" }
#=> #<Papercraft::Block::HTML:0x0...>
hello_world.render('World')
#=> "<h1>Hello, World!</h1>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment