Skip to content

Instantly share code, notes, and snippets.

@max-power
Last active March 29, 2024 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-power/4076bffde40882b309ed22a352925bd7 to your computer and use it in GitHub Desktop.
Save max-power/4076bffde40882b309ed22a352925bd7 to your computer and use it in GitHub Desktop.
Very simple HTML-Tags in Ruby
class Tag
def initialize(name, content = nil, **attributes, &block)
@name = name
@content = block_given? ? yield : content
@attributes = attributes
end
def to_s
"<#{@name}#{attributes}>#{@content}</#{@name}>"
end
private
def attributes
@attributes.map { |k,v| v.nil? ? k : %[#{k}="#{v}"] }.unshift('').join(' ')
end
end
#Tag.new(:ul, class: "power-list") do
# Tag.new(:li) do
# Tag.new(:a, href: "/help.html") { "Help" }
# end
# Tag.new(:li) do
# Tag.new(:a, "About", href: "/about.html")
# end
#end.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment