# Scope of an @variable

**URL:** <https://www.ruby-forum.com/t/scope-of-an-variable/54876>\
**Category:** Ruby\
**Created:** [March 14, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876 "2006-03-14T23:01:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 14, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/1 "2006-03-14T23:01:56Z")

</div>

I’ve got a class. I want some methods of this class to be able to edit  
some data that’s “global” within any given instance of this class. For  
example:

class Person

@name

def changeName(newName)  
@name = newName  
end

def sayName()  
puts "My name is " + @name  
end  
end

It seems that @name reverts back once I leave the scope of any method  
that manipulates it.

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 14, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/2 "2006-03-14T23:08:41Z")

</div>

you need to use @@name.

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 14, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/3 "2006-03-14T23:17:49Z")

</div>

Adam S. wrote:

> you need to use @@name.

I was under the impression (newbie alert) that @name was for instances  
and @@name was for classes as a whole (ie, @@ variables change that  
value in all instances of Class). Is there a way to have variables that  
apply to all instances of a Class?

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 15, 2006, 3:13am UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/4 "2006-03-15T03:13:47Z")

</div>

Hi –

On Wed, 15 Mar 2006, Nathan O. wrote:

> end
> 
> def sayName()  
> puts "My name is " + @name  
> end  
> end
> 
> It seems that @name reverts back once I leave the scope of any method  
> that manipulates it.

As you’ve learned from some of the other responses, @name is an  
instance variable. Each instance variable belongs to one object. You  
can always tell _which_ object: it’s whatever the default object  
(self) is, at the point where the “@var” is executed.

Note that self changes between a class definition and an instance  
method definition:

class Person  
puts self  
def some\_method  
puts self  
end  
end

Person.new.some\_method

This code will give you:

Person  
#Person:0x352814

In the top level of the class definition, self is the actual class  
object (Person), but inside an instance method, it’s the instance  
(indicated by the #\<Person…\> expression).

So… @name in the outer scope is actually an instance variable  
belonging to the class object, while inside any instance method, @name  
is an instance variable belonging to the instance. The two @name’s  
have no connection to each other at all.

You can use class variables to get a variable that can be seen in both  
scopes, but if you’ve got a property like “name” and you find yourself  
manipulating it outside of any instance method, something’s probably  
in need of tweaking in the program design (since the name of any  
particular instance shouldn’t be of concern at the class level).

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 15, 2006, 3:19am UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/5 "2006-03-15T03:19:50Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Note that self changes between a class definition and an instance  
> method definition:

Light bulbs floating above list readers’ heads around the world just  
illuminated.

Thanks for the great explanation!

–Steve

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 14, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/6 "2006-03-14T23:39:28Z")

</div>

On Mar 14, 2006, at 11:18 PM, Nathan O. wrote:

> I was under the impression (newbie alert) that @name was for instances  
> and @@name was for classes as a whole (ie, @@ variables change that  
> value in all instances of Class). Is there a way to have variables  
> that  
> apply to all instances of a Class?

Use an initialize method:

class Person  
def initialize(name=“Anonymous”)  
@name = name  
end  
attr\_accessor :name  
def to\_s  
“My name is #{@name}”  
end  
end

person = Person.new  
puts person # → My name is Anonymous  
person.name = “Fred”  
puts person # → My name is Fred

This should help you: [Programming Ruby: The Pragmatic Programmer's Guide](http://ruby-doc.org/docs/ProgrammingRuby)

– Daniel

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 15, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/scope-of-an-variable/54876/7 "2006-03-15T20:52:31Z")

</div>

On 3/14/06, Nathan O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Adam S. wrote:
> 
> > you need to use @@name.
> 
> I was under the impression (newbie alert) that @name was for instances  
> and @@name was for classes as a whole (ie, @@ variables change that  
> value in all instances of Class). Is there a way to have variables that  
> apply to all instances of a Class?

Oops, I read the question too quickly. Sorry. David Black has the  
right explanation.  
-Adam
