Forum: Ruby 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.
Tim U. (Guest)
on 2006-05-11 14:35
(Received via mailing list)
I was reading through the source code of the DBI module and I saw this
line there.

class << self

What does this statement do?
Jeff R. (Guest)
on 2006-05-11 14:54
(Received via mailing list)
I asked the same question yesterday...

It is just another way add singleton methods to the outer scope
(probably saw this in a module?).  So this:

module Foo
   class << self
     def a; end
     def b; end
   end
end

is the same as:

module Foo
   def Foo.a; end
   def Foo.b; end
end

I guess people use this style to organize sets of singleton methods so
that they are clearly separated from the rest of the module.  (Since
methods defined regularly won't be used until the module is mixed in
somewhere...)  Saves you from having to write the "Foo." part before
every method too...

-Jeff
Tim U. (Guest)
on 2006-05-11 15:06
(Received via mailing list)
> It is just another way add singleton methods to the outer scope
> (probably saw this in a module?).  So this:

Yes. I first tried to inherit from DBI but I couldn't because it's a
module. I then started reading it because it's a great way to learn a
language.

>
> module Foo
>    def Foo.a; end
>    def Foo.b; end
> end

Ok I get it. I guess this format is more familiar to my eyes but I can
see how the other way would be a little more economical and organized.
Raphael B. (Guest)
on 2006-05-11 15:24
(Received via mailing list)
On 5/11/06, Tim U. <removed_email_address@domain.invalid> wrote:
> >    def Foo.b; end
> > end
>
> Ok I get it. I guess this format is more familiar to my eyes but I can
> see how the other way would be a little more economical and organized.
>
Here's a very interesting read:
http://whytheluckystiff.net/articles/seeingMetacla...

I discovered it yesterday myself
Raph
unknown (Guest)
on 2006-05-11 16:37
(Received via mailing list)
Hi --

On Thu, 11 May 2006, Tim U. wrote:

> I was reading through the source code of the DBI module and I saw this
> line there.
>
> class << self
>
> What does this statement do?

In general, this:

   class << obj

puts you into a class definition block for the singleton class of obj.
The singleton class of obj is where those methods are stored that are
unique to obj.  So it's like a class-definition interface to obj's
unique behaviors.

class << self is just a case where the object whose singleton class
you're gaining access to is the current object (self).


David
Dave B. (Guest)
on 2006-05-11 20:10
(Received via mailing list)
Raphael B. wrote:
> Here's a very interesting read:
> http://whytheluckystiff.net/articles/seeingMetacla...

And here's a three-panel cartoon by the same author:
http://www.poignantguide.net/ruby/i/about.the.poig...

Cheers,
Dave
Florian GroÃ? (Guest)
on 2006-05-13 19:01
(Received via mailing list)
Tim U. wrote:

> Yes. I first tried to inherit from DBI but I couldn't because it's a
> module.

include() it instead. :)
Tim U. (Guest)
on 2006-05-14 09:32
(Received via mailing list)
> > Yes. I first tried to inherit from DBI but I couldn't because it's a
> > module.
>
> include() it instead. :)
>

I was going to do that but i still haven't quire wrapped my mind
around mix-ins. I decided on a container model and just declared a
database handle as an instance variable.

The thing that insterests me is the choice to write a module as
opposed to an object.  One day I will figure out when to write a
module instead of an object. For now I am just writing objects and
pretending that ruby is java or python. I figure that's good enough
till I learn more about mixins.
This topic is locked and can not be replied to.