# Re: Scope of an @variable

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

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [March 14, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/re-scope-of-an-variable/54879/1 "2006-03-14T23:42:30Z")

</div>

I’m also a newbie so others feel free to correct me as well 😉

Your understanding is correct in that @name applies to the specific  
instance of the class and the @@name applies to all instances of the  
class.

Hope this example helps.

class Person  
@@lastname = “Doe”  
@name=""

```
def setLastName(newName)
	@@lastname = newName
end

def changeName(newName)
	@name = newName
end

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

def fullName()
	"My full name is " + @name + " " + @@lastname
end

```

end

puts “—Defining John—\n”  
p1 = Person.new()  
p1.changeName(‘John’)  
puts p1.sayName()  
puts p1.fullName()

puts “\n”

puts “—Defining Jane—\n”  
p2 = Person.new()  
p2.changeName(‘Jane’)  
puts p2.sayName()  
puts p2.fullName()

puts “\n”

puts “—Changing Last Name—\n”  
p1.setLastName(‘Smith’)

puts "John’s full name -\> " + p1.fullName()  
puts "Jane’s full name -\> " + p2.fullName()

---

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [March 15, 2006, 1:16am UTC](https://www.ruby-forum.com/t/re-scope-of-an-variable/54879/2 "2006-03-15T01:16:42Z")

</div>

William (Bill) Froelich wrote:

> I’m also a newbie so others feel free to correct me as well 😉
> 
> Your understanding is correct in that @name applies to the specific  
> instance of the class and the @@name applies to all instances of the  
> class.

This may be wrong, but I like to think of @@ as a class variable and @  
as a method variable. @@ is accessible by any method in the class and @  
is accessible by the method only.

---

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [March 15, 2006, 1:54am UTC](https://www.ruby-forum.com/t/re-scope-of-an-variable/54879/3 "2006-03-15T01:54:30Z")

</div>

rtilley wrote:

> This may be wrong, but I like to think of @@ as a class variable and @  
> as a method variable. @@ is accessible by any method in the class and @  
> is accessible by the method only.

You’re close. @ are instance variables. Any instance method in your  
class can  
access them.

(Maybe you’re thinking of local variables, which are defined within a  
method and are accessible by that method only.)

Jeff

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

[www.jeffcohenonline.com](http://www.jeffcohenonline.com)

---

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [March 15, 2006, 2:06am UTC](https://www.ruby-forum.com/t/re-scope-of-an-variable/54879/4 "2006-03-15T02:06:22Z")

</div>

Jeff C. wrote:

> method and are accessible by that method only.)  
> Thanks Jeff! That makes sense. I was confusing @ vars with local vars.

---

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [March 17, 2006, 1:07am UTC](https://www.ruby-forum.com/t/re-scope-of-an-variable/54879/5 "2006-03-17T01:07:10Z")

</div>

On 15 Mar 2006, at 00:54, Jeff C. wrote:

> rtilley wrote:
> 
> > This may be wrong, but I like to think of @@ as a class variable  
> > and @  
> > as a method variable. @@ is accessible by any method in the class  
> > and @  
> > is accessible by the method only.
> 
> You’re close. @ are instance variables. Any instance method in your  
> class can  
> access them.

You can also access @ variables from a class method - ie  
ClassName.function; @a\_variable; end. They are local to each class  
within a hierarchy, rather than @@ which would be shared among  
classes in the hierarchy.

So:

class X  
def X.var;  
@var  
end

```
def X.var=(v)
	@var=v
end

```

end

class Y\<X;end

X.var=10  
Y.var=20  
X.var =\> 10  
Y.var =\> 20

Cheers,  
Benjohn

---

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [March 17, 2006, 2:13am UTC](https://www.ruby-forum.com/t/re-scope-of-an-variable/54879/6 "2006-03-17T02:13:42Z")

</div>

Hi –

On Fri, 17 Mar 2006, Benjohn B. wrote:

> > access them.  
> > end  
> > X.var =\> 10  
> > Y.var =\> 20

Nice, isn’t it? It’s really just this:

obj = Object.new  
def obj.var  
@var  
end

def obj.var=(v)  
@var = v  
end

with obj being a Class object. Plus the special-cased thing where the  
subclasses get to call it (because the singleton class of X is  
considered the superclass of the singleton class of Y).

You can also do:

class X  
class \<\< self # singleton class of X  
attr\_accessor :var  
end  
end

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)
