I was reading through the source code of the DBI module and I saw this line there. class << self What does this statement do?
on 2006-05-11 12:35
on 2006-05-11 12:54
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
on 2006-05-11 13:06
> 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.
on 2006-05-11 13:24
On 5/11/06, Tim Uckun <firstname.lastname@example.org> 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
on 2006-05-11 14:37
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
on 2006-05-11 18:10
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
on 2006-05-13 17:01
Tim Uckun wrote: > Yes. I first tried to inherit from DBI but I couldn't because it's a > module. include() it instead. :)
on 2006-05-14 07:32
> > 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.