# A little help needed

**URL:** <https://www.ruby-forum.com/t/a-little-help-needed/186239>\
**Category:** Ruby\
**Created:** [April 2, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239 "2010-04-02T21:42:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/1 "2010-04-02T21:42:44Z")

</div>

Hi,I’m new here.

I have a question:

What exactly does the ‘@’ symbol mean when used for the scope of a  
variable?  
For instance: @screen=[640,400].

I haven’t found anything explicitly explaining exactly what this does.

Thanks!

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/2 "2010-04-02T21:53:12Z")

</div>

On Fri, Apr 2, 2010 at 9:42 PM, Tom S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,I’m new here.
> 
> I have a question:
> 
> What exactly does the ‘@’ symbol mean when used for the scope of a  
> variable?  
> For instance: @screen=[640,400].
> 
> I haven’t found anything explicitly explaining exactly what this does.

It means it’s an instance variable of the object that is currently  
self in that context.  
For example:

class A  
def initialize  
@value = 3  
end  
end

a = A.new

The object referenced by a has an instance variable called @value with  
value 3.

Jesus.

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 9:56pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/3 "2010-04-02T21:56:43Z")

</div>

Oh! I see!

So it’s a variable that is automtically given to each instance of that  
class right?

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/4 "2010-04-02T21:59:04Z")

</div>

> So it’s a variable that is automtically given to each instance of that  
> class right?

Not quite… it’s a variable in the instance that it is declared. So,  
if you put it in the ‘initialize’ method as seen in that example, it  
will assign that variable for that instance when it is created.

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 10:15pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/5 "2010-04-02T22:15:53Z")

</div>

Jonathan N. wrote:

> > So it’s a variable that is automtically given to each instance of that  
> > class right?
> 
> Not quite… it’s a variable in the instance that it is declared. So,  
> if you put it in the ‘initialize’ method as seen in that example, it  
> will assign that variable for that instance when it is created.
> 
> -Jonathan N.

So… it’s a variable that can only be used in that instance and nowhere  
else?

such as:

Class Test

def initialize  
@x=5  
end

def testing  
puts"#{@x}"  
end  
end

problem=Test.new

problem.testing

> 5

#however

puts"#{@x}"

> error

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 10:25pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/6 "2010-04-02T22:25:25Z")

</div>

> problem=Test.new
> 
> problem.testing
> 
> > 5
> 
> #however
> 
> puts"#{@x}"
> 
> > error

Yes, it can only be used within that class. You can, however, create  
an accessor for it like this:

class Test  
def initialize  
@x = 5  
end

attr\_accessor :x  
end

solution = Test.new  
solution.x (=\>5)

solution.x = 6  
solution.x (=\>6)

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 2, 2010, 10:31pm UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/7 "2010-04-02T22:31:21Z")

</div>

Jonathan N. wrote:

> Yes, it can only be used within that class. You can, however, create  
> an accessor for it like this:
> 
> class Test  
> def initialize  
> @x = 5  
> end
> 
> attr\_accessor :x  
> end
> 
> solution = Test.new  
> solution.x (=\>5)
> 
> solution.x = 6  
> solution.x (=\>6)
> 
> -Jonathan N.

Yep, already knew that. Thanks for clearing this up for me. I’m creating  
a game for school you see so I needed to know exacly what that meant.

(Now I can understand more of what’s happening in source code) 😉

---

<div class="post-metadata">

**Author:** ![homer-16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@homer-16](https://www.ruby-forum.com/u/homer-16)\
**Post date:** [April 3, 2010, 2:44am UTC](https://www.ruby-forum.com/t/a-little-help-needed/186239/8 "2010-04-03T02:44:45Z")

</div>

On Fri, Apr 2, 2010 at 2:15 PM, Tom S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts"#{@x}"
> 
> > error

You can also just say @x, here, because puts will convert the objects it  
receives into strings by calling the to\_s method on them  
puts “#{@x}”  
puts @x.to\_s  
puts @x

In this case will all be the same, there is no need to interpolate  
strings  
here.

At least in 1.9.1 this is true. I tried it in 1.8 and the last one  
outputted  
“nil” which is odd, and the only way I can think of that would explain  
it is  
that puts checks to see if the variable is nil before calling to\_s, and  
explicitly outputs “nil” if it is.
