We know a lot about Ruby, and we are sure you know too. But practice makes perfect. In this article, we offer to check what you know about writing classes in Ruby, how to create them & why they’re so useful!
A Ruby class helps you define a blueprint for objects. An object is an individual “thing”, with its own identity & its own data.
- Class names start with an uppercase letter
- We use the
classkeyword, then the
- An empty class is not very useful, but you can still create objects from it
There are other ways to create a class (like
Class.new), but these methods are only useful in special situations.
The main use of a class is to be a container of methods, instance variables & constants, forming a blueprint which you can create objects from. Classes become more useful when you start adding methods & instance variables to them.
A method is a thing your class can do. If you know the class you can find out what methods are available (use Google,
pry), in other words, you can discover what the object can do for you!
Feel free to check more code examples here.
And explore 8 alternative Ruby frameworks! How many of them do you know?