Skip to content

Instantly share code, notes, and snippets.

@bmizerany
Forked from auser/gist:5867
Created August 17, 2008 23:29
Show Gist options
  • Save bmizerany/5868 to your computer and use it in GitHub Desktop.
Save bmizerany/5868 to your computer and use it in GitHub Desktop.
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