Execution question

What happens to ‘a’ in the following case?

class Test
a = 1
puts a
end

When executing ‘a’ is printed but what is ‘a’ in this case?

···


Justin Johnson

What happens to ‘a’ in the following case?

class Test
a = 1
puts a
end

When executing ‘a’ is printed but what is ‘a’ in this case?

Interesting question.

My interpretation may be wrong, but I’ll tell you
what I think.

Obviously ‘a’ is a variable. No one will deny that.

From its lack of a prefix ($, @, or @@) it must be
a local variable.

The only question is: Local to what? From its behavior,
it exists only within the scope of the class definition.
referenced from within an instance method of Test, nor
obviously from outside the class.

If I had to give it a name, I would call it a “class-level
local variable” or a “class-local variable.”

It is definitely neither a class variable nor an instance
variable.

It is not even the ever-weird “class instance variable” (and
I wish I had a better name for that) which you would get by
referencing a single-@ name (like @var) outside of any
method but inside the class.

Always remember that “declarations” in Ruby… really aren’t
declarations. They are executed. That is why you were able
to put an assignment and a “puts” inside the class definition
but OUTSIDE of any method definition.

A later “puts” of “a” at the same level, later on (after, say,
defining some methods) will reference the same “a” (as you
might expect).

I didn’t really ever stop to think that a class definition
started a new scope for local variables in this way. But
apparently it does.

It doesn’t seem useful to me… but please, prove me wrong. :slight_smile:

As a side note, if you “reopen” this class, you can’t access
this variable again (arguably making it even less useful – bug
or feature?). Apparently it has died and gone to what Matz calls
“variable heaven” (search ruby-talk archives). Example:

class Test
a = 1
puts a

def mymeth
end

puts a  # This works.

end

class Test
puts a # This doesn’t.
end

Thanks for bringing up this little issue.

Other comments, anyone? Matz, Dave, ts?

Hal Fulton

···

----- Original Message -----
From: “Justin Johnson” justinj@mobiusent.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 15, 2002 1:05 PM
Subject: Execution question

From my quick experiments, as I expected, it can’t be

Hi –

From: “Justin Johnson” justinj@mobiusent.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 15, 2002 1:05 PM
Subject: Execution question

What happens to ‘a’ in the following case?

class Test
a = 1
puts a
end

When executing ‘a’ is printed but what is ‘a’ in this case?

[…]

If I had to give it a name, I would call it a “class-level
local variable” or a “class-local variable.”

It is definitely neither a class variable nor an instance
variable.

It is not even the ever-weird “class instance variable” (and
I wish I had a better name for that) which you would get by
referencing a single-@ name (like @var) outside of any
method but inside the class.

I’d still call that an instance variable – bearing in mind that a
class is an instance of Class (which has helped me find it less weird,
too :slight_smile:

[…]

I didn’t really ever stop to think that a class definition
started a new scope for local variables in this way. But
apparently it does.

It doesn’t seem useful to me… but please, prove me wrong. :slight_smile:

Hmmm… I can’t make a great case, but maybe just for temp variables
in the service of creating non-temp things:

class Thing
a = 0
@@pr = proc { |x| a += x }

end

even though said “non-temp things” aren’t going to include method
definitions.

As a side note, if you “reopen” this class, you can’t access
this variable again (arguably making it even less useful – bug
or feature?). Apparently it has died and gone to what Matz calls

Feature, I think. It would be awfully weird to have local variables
suddenly exist when you reopened a class. (Not that it would be
likely to arise all that often, but still.)

David

···

On Tue, 16 Jul 2002, Hal E. Fulton wrote:

----- Original Message -----


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi,

···

At Tue, 16 Jul 2002 03:24:53 +0900, Hal E. Fulton hal9000@hypermetrics.com wrote:

“variable heaven” (search ruby-talk archives). Example:

Probably, it’s above the stack. :slight_smile:


Nobu Nakada

Hi,

···

In message “Re: Execution question” on 02/07/16, “Hal E. Fulton” hal9000@hypermetrics.com writes:

The only question is: Local to what? From its behavior,
it exists only within the scope of the class definition.
From my quick experiments, as I expected, it can’t be
referenced from within an instance method of Test, nor
obviously from outside the class.

It’s local to the scope formed by class body. Scopes do not nest
except ones by blocks. So it’s just a plain local variable.

						matz.

:slight_smile: Then… I do not want to know
what is below the heap…

Hal

···

----- Original Message -----
From: nobu.nokada@softhome.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 15, 2002 3:47 PM
Subject: Re: Execution question

Hi,

At Tue, 16 Jul 2002 03:24:53 +0900, > Hal E. Fulton hal9000@hypermetrics.com wrote:

“variable heaven” (search ruby-talk archives). Example:

Probably, it’s above the stack. :slight_smile:

Ah, I think I understand now.

The interpreter see’s the class definition and enters scope. The local
variable is created within that scope. When the class ‘end’ is read, the
scope finishes and the variable (providing it’s not referenced elsewhere by
then?) becomes a candidate for gc.

Effectively, the variable can only be seen within the scope of the class
definition.

That’s not so unusual after all. Thanks for clearing that up.

Justin Johnson.

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1026773406.615470.14300.nullmailer@picachu.netlab.jp…

···

Hi,

In message “Re: Execution question” > on 02/07/16, “Hal E. Fulton” hal9000@hypermetrics.com writes:

The only question is: Local to what? From its behavior,
it exists only within the scope of the class definition.
From my quick experiments, as I expected, it can’t be
referenced from within an instance method of Test, nor
obviously from outside the class.

It’s local to the scope formed by class body. Scopes do not nest
except ones by blocks. So it’s just a plain local variable.

matz.