-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_missing.rb
51 lines (42 loc) · 1.54 KB
/
method_missing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Source: https://www.codewithjason.com/ruby-method-missing/
# Takeaways
# 1. `method_missing` can be useful for constructing DSLs
# 2. `method_missing` can be added to any object to endow that object with special
# behavior when the object gets sent a message for which is doesn't have a method defined
# 3. `method_missing` takes the name of the method that was called, an arbitrary number of
# arguments, and (optionally) a block
class HTMLDocument
def initialize(&block)
# This instance_exec means that any message that's
# sent inside the HTMLDocument.new block (html, body,
# etc.) will use HTMLDocument as its recipient.
# See https://www.codewithjason.com/ruby-instance-exec/
instance_exec(&block)
end
private
def method_missing(method_name, *args, &block)
# args is equal to:
# [{:href=>"https://www.codewithjason.com", :target=>"_top"}]
# We're interested in the "first" (and only) element
puts "<#{method_name}#{hash_to_html_attributes(args[0])}>"
block.call
puts "</#{method_name}>"
end
def hash_to_html_attributes(hash)
return unless hash
stringified_attributes = hash.map do |key, value|
"#{key}=\"#{value}\""
end
" #{stringified_attributes.join(" ")}"
end
end
HTMLDocument.new do
html do
body do
puts "Hello world"
a({href: "https://www.codewithjason.com", target: "_top"}) do
puts "Code with Jason"
end
end
end
end