# Difference in public class methods and instance methods?

**URL:** <https://www.ruby-forum.com/t/difference-in-public-class-methods-and-instance-methods/146378>\
**Category:** Rails\
**Created:** [August 20, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/difference-in-public-class-methods-and-instance-methods/146378 "2008-08-20T13:36:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 20, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/difference-in-public-class-methods-and-instance-methods/146378/1 "2008-08-20T13:36:53Z")

</div>

What is difference between public class methods and public instance  
methods?  
I am interested in knowing in detail way.

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 20, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/difference-in-public-class-methods-and-instance-methods/146378/2 "2008-08-20T15:04:51Z")

</div>

On Wed, Aug 20, 2008 at 7:36 AM, Sunny B. \<  
removed\_email\_address@domain.invalid\> wrote:

> What is difference between public class methods and public instance  
> methods?  
> I am interested in knowing in detail way.

Class methods are called on the class, whereas instance methods are  
called  
on instances of the class.

class Foo  
def self.class\_method  
end

def instance\_method  
end  
end

Foo.class\_method  
Foo.new.instance\_method

Find more information here:  
[http://www.rubycentral.com/pickaxe/tut\_classes.html](http://www.rubycentral.com/pickaxe/tut_classes.html)

Brandon

* * *

Sessions by Collective Idea: Ruby on Rails training in a vacation  
setting  
[http://sessions.collectiveidea.com](http://sessions.collectiveidea.com)

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 20, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/difference-in-public-class-methods-and-instance-methods/146378/3 "2008-08-20T17:23:18Z")

</div>

“Public” refers to the accessibility of the methods–public methods can  
be called from anyplace. Compare private methods, which are only  
accessible from within the module where they are defined (or something  
close to that).

Instance methods are called on (and operate on) particular well,  
instances of a class. In this code:

x = “boogers”  
x.length # -\> 7

The .length method on the String class is an instance method. We are  
asking _this particular instance_ of String (which we have given the  
name ‘x’) how many characters it has.

Class methods are called on the class itself. For example, String.new  
is a class method–the class creates a new string and returns it. You  
couldn’t say x.new–x is just an instance of string. The only thing  
that can create new instances of String is the String class. In other  
languages these are often called ‘static’ methods.

For more detail, I recommend David Black’s _Ruby For Rails_, and/or the  
pickaxe book.

HTH,

-Roy
