with and without initialize method

L

Li Chen

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
 
S

Stefano Crocco

Alle 17:18, marted=C3=AC 9 gennaio 2007, Li Chen ha scritto:
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=3DX.new
a.hello_1


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

def hello_2
puts'Hello_2'
end
end

b=3DY.new
b.hello_2

######screen output

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

If you want to call methods from the class (X.a_method), you must define th=
em=20
this way:

class X
def X.a_method
#method implementation
end
end

X.a_method

I hope this helps

Stefano
 
L

Li Chen

Stefano said:
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
 
S

Stefano Crocco

Alle 17:51, marted=C3=AC 9 gennaio 2007, Li Chen ha scritto:
But when =C2=A0I 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=
=20
returns the names of the publicly accessible methods). To see it, you must=
=20
use private_methods:

rb(main):003:0> puts Object.private_methods.sort
=2E..
include
included
inherited
initialize
initialize_copy
irb_binding
iterator?
=2E..

Stefano
 
L

Li Chen

Stefano said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top