0

I am Intern Ruby on Rails Developer, who is willing to understand guts of core Ruby library and it's dependecies.

Traversing through it I came across a strange fact - in all the guides it was recommended not to use class variables in Ruby classes beacause of them being shared in all the inheritance chain below of class where we use such variable @@foo or whatever we call it and even classes where we me mixin the modules where we declared such class variables.

What is a purpose of such global scope of class variables visibility? Why it is not fixed to more safe behavior in one of the Ruby relases if community now considers a malicious practice to use class variables at all?

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • I assume Ruby Core team is skilled enough to create some fallbacks for such cases and do not pollute code of every Ruby developers with almost global scoped traps. – Artur Martsinkovskyi Nov 05 '16 at 11:20
  • Perhaps sometimes you need a class variable and sometimes a class hierarchy variable? – spickermann Nov 05 '16 at 11:20
  • Than what is the reason for them to be named class variables, not the class hierarchy variables? – Artur Martsinkovskyi Nov 05 '16 at 11:22
  • 2
    Class variables are essentially like global variables within the scope of all inherited classes, which can be a powerful feature. They're called `class variables` because that's how they behave in the simplest case (and possibly how they were first intended to be used)'. Rubyists with moderate experience are aware of how they work. As you can make `class instance` variables, you have complete flexibility of choosing which solution will give you the behavour you need. – SteveTurczyn Nov 05 '16 at 11:30
  • 3
    I would say the guides that say "not to use class variables" really should be reworded to say "this is one of the characteristics of class variables..." and then talk about alternatives if the behavour is not what's desired. A better way of saying "don't use class variables" is to say "be clear that this is how class variables work" – SteveTurczyn Nov 05 '16 at 11:33
  • Have a look at http://stackoverflow.com/a/1251422/7010554 – maij Nov 05 '16 at 13:58
  • 1
    ruby's a pretty permissive language, it doesn't try and save you from your self so much as something like java. – max pleaner Nov 05 '16 at 14:59
  • 1
    @SteveTurczyn completely agree. As soon as one needs to store something related to the whole class hierarchy (i.e. to have a collection of all created objects in the single place,) the class variable is a perfect choice. Nowadays there is a tendency to simplify things and many cool things that are “too complicated to understand” are just considered being a “bad practice” or even “code smell.” This is outrageous. – Aleksei Matiushkin Nov 05 '16 at 16:03
  • 3
    @mudasobwa, "code smell" is not necessarily a bad thing. Your code, for example, has the smell of freshly-baked bread. – Cary Swoveland Nov 05 '16 at 16:31
  • @CarySwoveland thanks, appreciate it. – Aleksei Matiushkin Nov 05 '16 at 19:12

0 Answers0