Tag Archive for 'module'

Ruby class methods — several ways to define them, and how to abstract them into a module/mixin

I learned/confirmed two things about Ruby class methods recently: several equivalent ways to define them, and how to abstract them into a module/mixin.

Ruby class, Class, and Module

irb(main):030:0> Class.class
=> Class
irb(main):031:0> Class.superclass
=> Module
irb(main):032:0> Module.class
=> Class
irb(main):033:0> Module.superclass
=> Object
irb(main):034:0> Object.class
=> Class
irb(main):035:0> Object.superclass
=> nil

So, should this (in particular the parts in bold) blow my mind, or is it just a standard Chicken-Egg situation that exists within the fundamental constructs of any programming language?

I know that for the most part I’ll never have to worry about what’s going on here, but I’d like to explain it to myself once before I forget about it.

  • Are any of these defined (or definable) by Ruby code?
  • What would be a “more accurate” name for the class Class? ClassDefinition? HumanRelevantClass?

UPDATE: okay, ri Class informs me that there is such a thing as a “meta-class”. Knowing that this exists and that I’m not going crazy satisfies me for now. In the future I may read up on the concept further, starting with this article.




Close
Powered by ShareThis
All of mp3