what's different between self.method and class << self?

Z

Zhenning Guan

we can define a instance method in lots way.
mostly, I saw people define like this:
1.
class A
def self.class_method
#some code here....
end
end

2.
class A
class << self
def class_method
#some code here....
end
end
end


==
the question confused me is what's the advantage of doing this in 2?
they'are different? which is your choose?
 
P

Patrick Doyle

[Note: parts of this message were removed to make it a legal post.]

we can define a instance method in lots way.
mostly, I saw people define like this:
1.
class A
def self.class_method
#some code here....
end
end

2.
class A
class << self
def class_method
#some code here....
end
end
end


==
the question confused me is what's the advantage of doing this in 2?
they'are different? which is your choose?
I asked this same question just a couple of weeks ago. The answers I was
given were:

Method 2:
1) Saves some typing
2) Has a slightly different mechanism for looking up class constants, which
99.99% of the time won't be noticed.

Other than that there are no differences between methods 1 & 2.

--wpd
 
Z

Zhenning Guan

Patrick said:
I asked this same question just a couple of weeks ago. The answers I
was
given were:

Method 2:
1) Saves some typing
2) Has a slightly different mechanism for looking up class constants,
which
99.99% of the time won't be noticed.

Other than that there are no differences between methods 1 & 2.

--wpd

what's the meaning of saves some typing????
can you give me a example?
 
P

Patrick Doyle

[Note: parts of this message were removed to make it a legal post.]
what's the meaning of saves some typing????
can you give me a example?

def self.method
blah
blah
blah
end

you can type (within the class << self clause)

def method
blah
blah
blah
end

I didn't say it would save you a _lot_ of typing, just some :)

--wpd
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top