"We have always been at war with Eurasia": Class Variables in Ruby
Class variables! What are they, even.
Class variables are variables whose value changes across instances! You can recognize them because they are prepended with @@
.
But that definition is pretty opaque. Let’s use an example of a time when information gets uniformly (and rapidly) changed!
With apologies to anyone who’s read 1984 more recently than – aw geez – almost 20 years ago, here’s a spoiler-less summary of what you need to know to follow along:
In 1984 there’s this concept called Doublethink, wherein the people manage to basically avoid contradictory thought. The government fights a perpetual war alongside or against one of two other states – Eurasia and Eastasia. The history books are re-written every time an alliance changes, hence the doublethink-inspired idiom, “We have always been at war with Eastasia.”
We can imagine this rewriting of history with Ruby’s class variables because changing the value of a class variable changes across all instances of that class. You see:
We can go back and forth like this indefinitely. (It’s a perpetual war, after all.) There isn’t a single instance of the class Doublethink that won’t have a uniform value for the class variable @@always_at_war_with
. Cool, right?
You can also do some Doublethink with instance variables, but that’s for another day:
Thanks to Richo for helping me understand class variables (finally)