Mr eel

I Don’t Like Ruby’s Class Variables

Just a general comment. The behaviour of class variables in Ruby is nonsensical.

I’ve had many frustrating moments dealing with the way class variables are handled in sub-classes — they’re shared between all subclasses, rather than belonging to just the one class — now I’m having fun trying to create them when I mix in a module.

What they do and how they are scoped is confusing. Definitely one of my least favourite parts of the language.

Posted on January 9th, 2008 | There are 2 comments

Comments

Blake on January 9, 2008

Just use instance variables on your classes instead of class variables. Class instance variables are not shared by subclasses and you can manipulate them from mixed in modules straight-forwardly.

Why show’s off class instance var’s in his amusing Dwemthy’s Array post.

Mr eel on January 9, 2008

After a little bit of searching I discovered the use of class instance variables, so thankfully I managed to sort that out.

My confusion stems from the fact that the class variables in Ruby don’t behave in the same way you’d expect compared to other languages. Different is OK, but not when you’re using the same nomenclature. Ruby needs classic class variables and to adopt different terms to describe the current behaviour.

Or… um, so I’d like to humbly suggest :)

Add a comment

All contents © 2005—2007 Luke Matthew Sutton