# About class variable question?

**URL:** <https://www.ruby-forum.com/t/about-class-variable-question/147492>\
**Category:** Ruby\
**Created:** [September 2, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492 "2008-09-02T14:19:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 2, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/1 "2008-09-02T14:19:14Z")

</div>

Please see this code below. It will get “can’t convert nil to string  
error”. Why can’t the @name be assigned? I have set the @name = “world”.

class Hello  
@name=“world”  
def say  
puts "Hello "+@name  
end  
end  
h=Hello.new  
h.say

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 2, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/2 "2008-09-02T14:23:53Z")

</div>

On Tuesday 02 September 2008, Zhao Yi wrote:

> h.say  
> @name is an instance variable of the Hello object, not of instances of  
> Hello,  
> such as h. To create an instance variable of instances of Hello, you  
> need to  
> assign it a value in an instance method. Usually, this is done in the  
> initialize method:

class Hello

def initialize  
@name = “Hello”  
end

def say  
puts “Hello” + @name  
end

end

h = Hello.new  
h.say

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 2, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/3 "2008-09-02T14:52:00Z")

</div>

Stefano C. wrote:

> On Tuesday 02 September 2008, Zhao Yi wrote:
> 
> > h.say  
> > @name is an instance variable of the Hello object, not of instances of  
> > Hello,  
> > such as h. To create an instance variable of instances of Hello, you  
> > need to  
> > assign it a value in an instance method. Usually, this is done in the  
> > initialize method:
> 
> class Hello
> 
> def initialize  
> @name = “Hello”  
> end
> 
> def say  
> puts “Hello” + @name  
> end
> 
> end
> 
> h = Hello.new  
> h.say
> 
> I hope this helps
> 
> Stefano

If the name is Hello object instance, it should be accessed by Hello  
object, right? see this code:

class Hello  
@name=“world”

def name  
@name  
end  
end  
h=Hello.new  
puts h.name

It will print “nil” which means the first line of this class  
@name=“world” is ignored.  
Am I right?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 2, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/4 "2008-09-02T15:29:38Z")

</div>

Zhao Yi wrote:

> Please see this code below. It will get “can’t convert nil to string  
> error”. Why can’t the @name be assigned? I have set the @name = “world”.
> 
> class Hello  
> @name=“world”  
> def say  
> puts "Hello "+@name  
> end  
> end  
> h=Hello.new  
> h.say

Try it this way:

class Hello  
@@name=“world”  
def say  
puts "Hello "+@@name  
end  
end  
h=Hello.new

h.say

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 2, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/5 "2008-09-02T15:06:46Z")

</div>

On Tue, Sep 2, 2008 at 2:46 PM, Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > class Hello
> 
> class Hello  
> @name=“world” is ignored.  
> Am I right?

It’s not ignored. When you see @name=“something”, you have to think  
which object is self at that point. That object will have an instance  
variable  
called @name with that value. The thing is that classes are also  
objects (instances  
of class Class), and can have instance variables as any other object.  
This:

class Hello  
@name = “hello’s name”  
end

creates an instance variable of the object Hello. To achieve what you  
want you need to do as Stefano showed: create the @name instance  
variable  
in a place where self is the instance of the Hello class: inside a  
method, like initialize.

Jesus.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 3, 2008, 2:43am UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/6 "2008-09-03T02:43:38Z")

</div>

This is really different with Java at this point.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 3, 2008, 8:38am UTC](https://www.ruby-forum.com/t/about-class-variable-question/147492/7 "2008-09-03T08:38:04Z")

</div>

HI –

On Wed, 3 Sep 2008, Zhao Yi wrote:

> This is really different with Java at this point.

Yes – it’s a totally different language. Enjoy it 🙂

David
