From: Brian Candler Date: 2009-04-15T19:17:43+09:00 Subject: Re: Proc.call with custom bindings Adam Strzelecki wrote: > So think this explains my problem: > ---- cut ---- > proc = Proc.new do > if defined? s > puts "s = #{s}" > else > puts "s is undefined" > end > end > proc.call > s = 1 > proc.call > ---- cut ---- > > We got twice "s is undefined" even it is defined at second call. The local variable/method ambiguity is resolved statically when code is parsed, before anything is executed. Example: def x "hello" end if false x = "world" end puts x # nil At the "puts x", within the current scope there has been a (potential) assignment to x, and therefore it is already decided that x is a local variable. When it is executed, no assignment actually took place, so it carries the default value nil. Similarly: puts y # NameError y = nil There has been no (potential) assignment to y lexically before the puts statement, and therefore it is statically decided that y must be a method call, i.e. self.y() This logic may be a little surprising at first, but it means that you don't have to declare variables, and it also means you don't have to provide an empty set of parentheses when invoking a method which takes no arguments. Regards, Brian. -- Posted via http://www.ruby-forum.com/.