Project

General

Profile

This project is closed and read-only.

Actions

Bug #2362

closed

undefined variable has value?

Bug #2362: undefined variable has value?

Added by vo.x (Vit Ondruch) almost 17 years ago. Updated over 15 years ago.

Status:
Rejected
Assignee:
-
[ruby-core:26722]

Description

=begin
C:>type test.rb
if false
a = 5
else
end

c = a
d = b

C:>c:\ruby\bin\ruby.exe test.rb
test.rb:7:in <main>': undefined local variable or method b' for main:Object (NameError)

From my point of view, the variable "a" was never reached. How that can be that it is later defined with nil value?
=end

Updated by rue (Eero Saynatkari) almost 17 years ago Actions #1

=begin
Excerpts from message of Fri Nov 13 15:05:19 +0200 2009:

From my point of view, the variable "a" was never reached. How that can be that
it is later defined with nil value?

Technically, nil denotes the absence of a value.

This is a parsing artifact, I think...Is there a case
where this actually causes a problem?

Eero

Magic is insufficiently advanced technology.

=end

Updated by vo.x (Vit Ondruch) almost 17 years ago Actions #2

=begin
Yes, it caused problem in one of our applications. Of course it was connected with our oversight. Nevertheless, if it worked the same way for a and b, we would be warned.
=end

Updated by dblack (David Black) almost 17 years ago Actions #3

=begin
Hi --

On Fri, 13 Nov 2009, Vit Ondruch wrote:

Issue #2362 has been updated by Vit Ondruch.

Yes, it caused problem in one of our applications. Of course it was connected with our oversight. Nevertheless, if it worked the same way for a and b, we would be warned.

This is well-known and well-documented. It happens because the parser
sees the expression:

a = 5

and allocates a local variable for it.

David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

=end

Updated by murphy (Kornelius Kalnbach) almost 17 years ago Actions #4

=begin
David A. Black wrote:

This is well-known and well-documented. It happens because the parser
sees the expression:

a = 5

and allocates a local variable for it.
You could also say that = is Ruby's "var". At least I understand it as such.

[murphy]

=end

Updated by nobu (Nobuyoshi Nakada) almost 17 years ago Actions #5

  • Status changed from Open to Rejected

=begin
It is rather strange if these worked differently.

if foo()
a = 5
end

if 1 == 0
a = 5
end

if false
a = 5
end

=end

Actions

Also available in: PDF Atom