Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Last active August 10, 2020 17:37
Show Gist options
  • Save joalbertg/ab886bc5584507de7a465545c0efcb96 to your computer and use it in GitHub Desktop.
Save joalbertg/ab886bc5584507de7a465545c0efcb96 to your computer and use it in GitHub Desktop.
ruby: Metaprogramming
class HTML
def self.respond_to_missing?(method_name, *args)
# TODO: completar
end
def self.method_missing(method_name, *args, &block)
method_name ? tag(method_name, *args, &block) : super
end
def self.tag(tag_name, *args, &_block)
"<#{tag_name}>#{args.last} #{yield if block_given?}</#{tag_name}>"
end
end
html = HTML.div do
HTML.header do
HTML.h1 'Titulo de la página'
end + HTML.article { HTML.p 'Hola a todos' }
end
puts html
# <div>
# <header>
# <h1>Titulo de la página</h1>
# </header>
# <article>
# <p>Hola a todos</p>
# </article>
# </div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment