Created
October 14, 2025 18:51
-
-
Save havenwood/70761da84d19fd92b0f7452aa4f722d4 to your computer and use it in GitHub Desktop.
My suggestion for a lambda-like strict arity alternative to a block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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