Skip to content

Instantly share code, notes, and snippets.

@mbklein
Created November 16, 2011 21:59
Show Gist options
  • Save mbklein/1371593 to your computer and use it in GitHub Desktop.
Save mbklein/1371593 to your computer and use it in GitHub Desktop.
Why does key = value work in some cases, but not others?
c = Confstruct::Configuration.new(:foo => {})
c.configure do |c1|
# the block expects one param, so we've remained bound to
# the outer scope and yielded c.
# IOW, self == whatever self was outside the block, and c1 == c.
c1.x 123 # calls c1.method_missing(:x, 123)
c1.y = 456 # calls c1.method_missing(:y=, 123)
c1.foo.bar = 789 # calls ca.foo.method_missing(:bar=, 789)
end
=> {:x=>123, :y=>456, :foo=>{:bar=>789}}
c = Confstruct::Configuration.new(:foo => {})
c.configure do
# the block expects no params, so we're in c's instance
# scope right now. IOW, self == c.
x 123 # calls self.method_missing(:n, 123)
y = 456 # local assignments take precedence, so since c has
# no method called #y=, this just assigns
# the value 456 to a block-local variable called y
foo.bar = 789 # calls self.foo.method_missing(:bar, 789)
end
=> {:x=>123, :foo=>{:bar=>789}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment