Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what's different between self.method and class << self?

Reply
Thread Tools

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

 
 
Zhenning Guan
Guest
Posts: n/a
 
      10-21-2008
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Patrick Doyle
Guest
Posts: n/a
 
      10-21-2008
[Note: parts of this message were removed to make it a legal post.]

On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan <(E-Mail Removed)>wrote:

> 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

 
Reply With Quote
 
 
 
 
Zhenning Guan
Guest
Posts: n/a
 
      10-21-2008
Patrick Doyle wrote:
> On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan
> <(E-Mail Removed)>wrote:
>
>> class A
>> 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


what's the meaning of saves some typing????
can you give me a example?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Patrick Doyle
Guest
Posts: n/a
 
      10-21-2008
[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?
> --
> Posted via http://www.ruby-forum.com/.
>
> Instead of having to type


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
What the different between form ".this" and ".class"? Bruce Sam Java 2 12-03-2004 06:15 AM
what's the difference between the different the different GraphicConfigurations Flip Java 0 07-30-2004 03:50 PM



Advertisments