Declaring classes in Ruby
Both PHP and Ruby are languages that use the Object-Oriented Programming (OOP) paradigm, Ruby by design and PHP by its own evolution. By now, serious developers should be very familiar with the paradigm. In PHP, all frameworks use OOP. While we are not going to go in depth into how this paradigm is implemented in Ruby, we will go through the basics of class syntax.
A class is basically an abstraction of a real-world entity. It is the blueprint of this abstraction. Let’s start by creating a simple class representing a person, some attributes for this person, and an action (or method) for them. Let’s create a file called class_syntax.rb
with the following content:
class Person end
This is as simple as it gets, but this by itself is not very useful. For this to be useful, we need to add attributes that represent the characteristics of a person. So, let’s add some attributes such as their first name and their last name. Our code will...