Created
November 28, 2015 22:46
-
-
Save domgetter/6185c43ebec14f9a7aef to your computer and use it in GitHub Desktop.
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 Integer | |
def tags(&block) | |
-> {self.times.reduce([]) {|a, i| a << block.yield(i)}} | |
end | |
end | |
class String | |
def to_html | |
self | |
end | |
end | |
class Proc | |
def to_html | |
self.call.reduce("") {|str,e| str + e.to_html} | |
end | |
end | |
class Array | |
def to_html | |
str = "" | |
type = self.first | |
self[1..-1].each do |child| | |
str += child.to_html | |
end | |
str = "<#{type}>" + str + "</#{type}>" | |
end | |
end | |
[:span, "hello"].to_html | |
#=> "<span>hello</span>" | |
my_div = [:div, [:ul, 10.tags {[:li, "hello"]}]] | |
my_div.to_html | |
#=> "<div><ul><li>hello</li><li>hello</li><li>hello</li><li>hello</li><li>hello</li><li>hello</li><li>hello</li><li>hello</li><li>hello</li><li>hello</li></ul></div>" | |
[:div, | |
10.tags do |num| | |
[:span, num.to_s] | |
end | |
].to_html | |
#=> "<div><span>0</span><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span></div>" | |
# Referential transparency! | |
two_a = 2.tags {[:a, "google.com"]} | |
[:body, two_a].to_html | |
#=> "<body><a>google.com</a><a>google.com</a></body>" | |
# Now to work on attributes | |
# Here's how it should work: | |
# [:span, {id: "main", class: "blue"}, "hello"].to_html | |
# #=> "<span id='main' class='blue'>hello</span>" | |
# But we're not there yet... Stay tuned! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment