# With and without initialize method

**URL:** <https://www.ruby-forum.com/t/with-and-without-initialize-method/81524>\
**Category:** Ruby\
**Created:** [January 9, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/with-and-without-initialize-method/81524 "2007-01-09T17:18:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/with-and-without-initialize-method/81524/1 "2007-01-09T17:18:19Z")

</div>

Hi all,

I try to understand some concepts in Ruby. I define two classes, one  
with and the other without initialize method. In order to call the  
methods defined in each class I need to call new/initialize method  
first. Why is that?

Thanks,

Li

#####################  
class X  
def hello\_1  
puts ‘Hello\_1’  
end  
end

a=X.new  
a.hello\_1

####################  
class Y  
def initialize  
end

def hello\_2  
puts’Hello\_2’  
end  
end

b=Y.new  
b.hello\_2

######screen output

> ruby test3.rb  
> Hello\_1  
> Hello\_2  
> Exit code: 0

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/with-and-without-initialize-method/81524/2 "2007-01-09T17:31:10Z")

</div>

Alle 17:18, martedÃ¬ 9 gennaio 2007, Li Chen ha scritto:

> Li
> 
> > Exit code: 0  
> > The methods you defined are instance methods, i.e they must be called  
> > using  
> > instances of the class as receiver; you can’t call them from the class  
> > themselves. You need to call new because new returns an instance of the  
> > class. In the first case, where you don’t define initialize, the  
> > initialize  
> > method of the base class (Object) is called.

If you want to call methods from the class (X.a\_method), you must define  
them  
this way:

class X  
def X.a\_method  
#method implementation  
end  
end

X.a\_method

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/with-and-without-initialize-method/81524/3 "2007-01-09T17:51:33Z")

</div>

Stefano C. wrote:

> The methods you defined are instance methods, i.e they must be called  
> using  
> instances of the class as receiver; you can’t call them from the class  
> themselves. You need to call new because new returns an instance of the  
> class. In the first case, where you don’t define initialize, the  
> initialize  
> method of the base class (Object) is called.

Thanks and it helps. But when I check which methods are available in  
class Object I can’t see initialize there. Do I miss something?

Li

irb(main):003:0\> puts Object.methods{|m|m.to\_s}.sort  
…  
id  
include?  
included\_modules  
inspect  
instance\_eval  
instance\_method  
instance\_methods  
instance\_of?  
instance\_variable\_get  
instance\_variable\_set  
instance\_variables  
is\_a?  
…  
=\> nil

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/with-and-without-initialize-method/81524/4 "2007-01-09T18:12:37Z")

</div>

Stefano C. wrote:

> > 
> 
> initialize is a private method, so it won’t be shown by methods (which  
> only  
> returns the names of the publicly accessible methods). To see it, you  
> must  
> use private\_methods:
> 
> rb(main):003:0\> puts Object.private\_methods.sort

Thank you very much,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/with-and-without-initialize-method/81524/5 "2007-01-09T18:04:14Z")

</div>

Alle 17:51, martedÃ¬ 9 gennaio 2007, Li Chen ha scritto:

> But when Â I check which methods are available in  
> class Object I can’t see initialize there. Do I miss something?

initialize is a private method, so it won’t be shown by methods (which  
only  
returns the names of the publicly accessible methods). To see it, you  
must  
use private\_methods:

rb(main):003:0\> puts Object.private\_methods.sort  
…  
include  
included  
inherited  
initialize  
initialize\_copy  
irb\_binding  
iterator?  
…

Stefano
