Last active
August 10, 2020 17:37
-
-
Save joalbertg/ab886bc5584507de7a465545c0efcb96 to your computer and use it in GitHub Desktop.
ruby: Metaprogramming
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
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