Defining and Instantiating Classes
Learn how to define our own classes and create class instances.
Let’s start by creating a Calculator
class and adding some methods to it, step by step.
In Ruby, we define a class like this:
Press + to interact
Ruby
class Calculatorend
That’s all. It’s not a very useful class because it’s completely empty, but it’s a class.
Remember: A class is defined using the class
keyword, a name, and the end
keyword.
Use capital letters to define classes
Notice also that the class has the name Calculator
, which starts with an uppercase letter. In Ruby, this is required, and we’d get an error if we tried to define a class called ...