-
-
Save bmizerany/5868 to your computer and use it in GitHub Desktop.
This file contains 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 Doc | |
def method_missing(m, *args, &block) | |
args.empty? ? options[m] : options[m] = args[0] | |
end | |
def options | |
@options ||= {} | |
end | |
def initialize(&block) | |
self.instance_eval(&block) if block | |
end | |
end | |
d = Doc.new do | |
hi "you" | |
mydoc(Doc.new do | |
who "me" | |
end) | |
end | |
# When instance evaluating within the d Doc, hi is sent "you" as args | |
# but inside the block, mydoc's who is NOT sent "me." Instead, it is | |
# sent nil | |
puts d.hi # => you | |
puts d.mydoc # => #<Doc:0x338308> | |
puts d.mydoc.who # => nil ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment