# Instance variables

**URL:** <https://www.ruby-forum.com/t/instance-variables/160683>\
**Category:** Rails\
**Created:** [February 18, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/instance-variables/160683 "2009-02-18T19:54:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [February 18, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/instance-variables/160683/1 "2009-02-18T19:54:33Z")

</div>

Learning about instance variables and don’t understand why the  
instance variable “name” in the code example below is not visible  
where as “title” is. Could someone explain this to me and why I hadto  
use the “self.get\_name()” method (which I don’t understand since I  
pulled it off the net) to get the value out of name?

## Thanks

class InstanceVariables  
@name = “Steve”

def initialize()  
@title = “Software Eng”  
end

def self.get\_name  
@name  
end

def get\_name  
self.class.get\_name # Why can’t I just return @name?  
end

def get\_title  
@title  
end

## def show\_vars puts “name: #@name, title: #@title” # name is not set, at least that I can tell. end end

## obj1 = InstanceVariables.new puts obj1.get\_name() puts obj1.get\_title() obj1.show\_vars()

Steve  
Software Eng  
name: , title: Software Eng

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [February 18, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/instance-variables/160683/2 "2009-02-18T19:57:31Z")

</div>

On 18 Feb 2009, at 18:42, Steve wrote:

> Learning about instance variables and don’t understand why the  
> instance variable “name” in the code example below is not visible  
> where as “title” is. Could someone explain this to me and why I hadto  
> use the “self.get\_name()” method (which I don’t understand since I  
> pulled it off the net) to get the value out of name?

Classes are objects in ruby and can have instance variables, so when  
you set @name = “Steve” where you have put it you’re create an  
instance variable on the class rather than an instance variable on a  
particular instance of the class.

Fred

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [February 19, 2009, 1:51am UTC](https://www.ruby-forum.com/t/instance-variables/160683/3 "2009-02-19T01:51:24Z")

</div>

So what is the distinction between a class instance variable and a  
class variable (ie @@name=“Steve”)?

On Feb 18, 1:56 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [February 19, 2009, 8:47am UTC](https://www.ruby-forum.com/t/instance-variables/160683/4 "2009-02-19T08:47:23Z")

</div>

On Feb 19, 12:50 am, Steve [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So what is the distinction between a class instance variable and a  
> class variable (ie @@name=“Steve”)?

mostly the odd scoping stuff which means that @@foo in an instance  
method and @@foo in a class context refer to the same object.

Fred
