From: ChrisH Date: 2007-04-06T03:10:12+09:00 Subject: Re: Why is local variable created in one branch of if statement available in the other? On Apr 5, 1:49 pm, "Alexey Verkhovsky" wrote: > I find it hard to understand why this code prints nil instead of > saying "unknown method or local variable foo": > > > cat hmm.rb > > def hmm > if false > foo = 'quack' > else > p foo > end > end > > hmm> ruby hmm.rb > > nil > > Thoughts? > > -- > Alex Verkhovsky The variable 'foo' is created during parsing stage so exists as "foo = 'quack'" has been parsed. Execution then results in the assignment being skipped so foo has the default nil value. Cheers Chris