Created
August 17, 2008 23:27
-
-
Save auser/5865 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_given? | |
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