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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ffd881829651935d986c52d12279befb?d=identicon&s=25 Zhenning Guan (rubyernewbie)
on 2008-10-21 09:24
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?
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-10-21 14:22
(Received via mailing list)
On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan
<g.zhen.ning@gmail.com>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
Ffd881829651935d986c52d12279befb?d=identicon&s=25 Zhenning Guan (rubyernewbie)
on 2008-10-21 14:27
Patrick Doyle wrote:
> On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan
> <g.zhen.ning@gmail.com>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?
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-10-21 14:33
(Received via mailing list)
>
> 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
Cec345a59245af9d06e4438a413f4eb5?d=identicon&s=25 Shot (Piotr Szotkowski) (Guest)
on 2008-10-21 21:38
(Received via mailing list)
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?

You can work on the singleton’s properties with attr_* methods:

class Example
  class << self
    attr_accessor :prop
  end
end

(With the first approach you’d have to define self.prop and
self.prop= to be able to write code like Example.prop = 'value'.)

-- Shot
Cec345a59245af9d06e4438a413f4eb5?d=identicon&s=25 Shot (Piotr Szotkowski) (Guest)
on 2008-10-21 22:09
(Received via mailing list)
Shot (Piotr Szotkowski):

> (With the first approach you’d have to define self.prop and
> self.prop= to be able to write code like Example.prop = 'value'.)

…or use something like Rails’ cattr_* accessors – but they work on
class variables (as opposed to instance variables of the singleton.)

-- Shot
This topic is locked and can not be replied to.