# Class Variable

**URL:** <https://www.ruby-forum.com/t/class-variable/95308>\
**Category:** Ruby\
**Created:** [May 27, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/class-variable/95308 "2006-05-27T17:37:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 27, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/class-variable/95308/1 "2006-05-27T17:37:36Z")

</div>

Perl has me all turned around with ruby classes.

I see some classes use a ‘self.’ and others don’t. When to use?

Also what does an @ do in front of a variable?

I think i understand private methods basically they are things only used  
with in the class and never return a value or effect anything beyond  
that  
correct?

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 27, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/class-variable/95308/2 "2006-05-27T18:08:03Z")

</div>

On 5/27/06, Paul D. Kraus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Perl has me all turned around with ruby classes.
> 
> I see some classes use a ‘self.’ and others don’t. When to use?

self.methodName creates a Class/Module method

Also what does an @ do in front of a variable?

@variable is an instance variable.

I think i understand private methods basically they are things only used

> with in the class and never return a value or effect anything beyond that  
> correct?

private methods _cannot_ be called by objects that are not an instance  
of  
a class that defines that private method.

You might find this tutorial/online book helpful:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/index.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/index.html)

-Madan

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 27, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/class-variable/95308/3 "2006-05-27T18:44:38Z")

</div>

Hi –

On Sun, 28 May 2006, Madan M. wrote:

> On 5/27/06, Paul D. Kraus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> private methods _cannot_ be called by objects that are not an instance of  
> a class that defines that private method.

You’re describing protected methods, not private methods. (See my  
previous post for comments on private methods.)

David

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 27, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/class-variable/95308/4 "2006-05-27T18:41:31Z")

</div>

Hi –

On Sun, 28 May 2006, Paul D. Kraus wrote:

> Perl has me all turned around with ruby classes.
> 
> I see some classes use a ‘self.’ and others don’t. When to use?

‘self’ is the default object, a role that rotates among different  
objects at different points in your program. When, and even whether,  
you use it explicitly depends on what you’re trying to do.

If you want to add a singleton method (i.e., a method for one specific  
object) to the object that is self, you would do:

def self.method\_name  
…  
end

That construct is very commonly used for creating class methods:

class C  
def self.some\_method  
…  
end  
end

but you can do it with any object, and in fact it’s just a  
not-very-special case of:

def some\_object.some\_method

which is how you would add a singleton method to some\_object.

Since self is the default object, when you call a method, you normally  
don’t have to specify self as the receiver:

class C  
def x  
puts “Hi”  
end

```
 def y
   x # effectively the same as: self.x
 end

```

end

However, when you use methods that end with = (equal sign), you do  
have to use an explicit ‘self’ receiver, to distinguish the method  
call from a local variable assignment. This is the (small) price paid  
for the nice syntactic sugar that comes with =-terminated methods:

class C  
def x=(n)  
@x = n  
end

```
 def y
   self.x = 10 # calls method x= with argument 10
   x = 10 # sets local variable x
 end

```

end

> Also what does an @ do in front of a variable?

@variable is an instance variable. Whenever you see @variable, you’re  
seeing an instance variable that belongs to whatever object is ‘self’  
at that moment.

> I think i understand private methods basically they are things only used  
> with in the class and never return a value or effect anything beyond that  
> correct?

No; the difference between a private method and a public method is  
that a private method cannot be called with an explicit receiver. You  
cannot do this:

c = C.new  
c.z

if z is a private instance method of c. That means that you can only  
do the equivalent of c.z under circumstances where ‘self’ is c –  
because, under such circumstances, a simple call to z will be  
equivalent to c.z (as in the earlier example). So generally you only  
see calls to private methods inside _other_ methods in the same class:

class C  
def x  
puts “Hi”  
end

```
 private :x

 def y
   x # OK, because no explicit receiver; it uses self
 end

```

end

(There are a couple of special cases etc. but that’s the bulk of it.)

David
