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.
C0cb3aadb9a66d00df16686f4a1fc312?d=identicon&s=25 Tim Uckun (Guest)
on 2006-05-11 12: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?
567898c496278341be69087507d5ed24?d=identicon&s=25 Jeff Rose (Guest)
on 2006-05-11 12: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
C0cb3aadb9a66d00df16686f4a1fc312?d=identicon&s=25 Tim Uckun (Guest)
on 2006-05-11 13: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.
71c85a662af0764df3a77cc00db166c9?d=identicon&s=25 Raphael Bauduin (Guest)
on 2006-05-11 13:24
(Received via mailing list)
On 5/11/06, Tim Uckun <timuckun@gmail.com> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-05-11 14:37
(Received via mailing list)
Hi --

On Thu, 11 May 2006, Tim Uckun 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
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-05-11 18:10
(Received via mailing list)
Raphael Bauduin 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
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-05-13 17:01
(Received via mailing list)
Tim Uckun wrote:

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

include() it instead. :)
C0cb3aadb9a66d00df16686f4a1fc312?d=identicon&s=25 Tim Uckun (Guest)
on 2006-05-14 07: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.