If you think about it, classes are a way of packaging up data and behavior. Instances created of a class are distinct from each other. In other words, each instance has its own state. Closures also provide that functionality. Let’s see how with an example.
Say you want to build a very simple counter program. The counter program can do the following:
This is the essence of what most classes do: retrieve and modify data. Here’s one possible implementation of a Counter class:
|||@x = 0|
|||@x += 1|
|||def decr ...|