2025-01-28
So far we have enough theoretical knowledge to understand Classes and Objects in Ruby. If you have been following previous blog post, here we have the order to study this subject
- - Introduction to Object-Oriented Programming in Ruby
- - Methods and Attributes in Object-oriented programming in Ruby
- - Ruby Getters and Setters in Object-oriented Programming
Just to recap, let’s see this important concepts
- - A Class is a “template” to create Objects
- - An Object is an “instance” of a Class
- - Classes can have the “initialize” method (commonly known as “The Constructor”) who is called each time an Object is created
- - The Constructor is commonly used to “initialize” object attributes
- - Inside the Class we can define Methods (the behavior) and Attributes (the information) that Objects will have. Methods are usually called “Instance Methods” and Attributes is commonly called “Instance Variables”
- - Attributes are info that is associated to the Object
- - Attributes are “private” by default and can just be read and modified inside the Object, unless you create methods to Read (getters) and Write (setters) outside the Object
- - You can use the “accessors” called “attr_accesor” (to read and write), attr_reader (to read) and attr_writter (to write) and Ruby will create, for us, the methods to read and write
As you can see we’ve advanced a lot in OOP and you should have, above concepts at hand, to solve next challenges
Challenges
Challenge #1
Write a class called Dog. The class must have a single “bark” method that returns "woof-woof".
Challenge #2
- Create a class called “Square”
- Add a constructor to “Square” who receives an argument called “length” and assign it to an attribute with the same name
- Add methods to read and write the “length” attribute
- Add a method called “area” that returns the square’s area
- Add a method called “perimeter” that returns the square’s perimeter
- Create 2 “Square” instances, first one with a longitude of 5 and second one with a longitude of 10. Check that it returns the correct data
Challenge #3
Your mission in this challenge is to write a class called Car that behaves like a simplified car that allows us to accelerate, brake and obtain its current speed. The Car class must be usable as follows:
car = Car.new
car.velocity # => 0
car.accelerate
car.velocity # => 1
car.accelerate(2)
car.velocity # => 3
car.brake
car.velocity # => 2
car.brake(2)
car.velocity # => 0
Note: It should not be possible to assign the speed of the car directly.
car.velocity = 100 # Should fail.
Instructions
- Create the Car class (we suggest you do this on your machine first so you can test it).
- Add the necessary methods and attributes to implement the class.
Challenge #4
- - Create a class called GoodDog with an initializer that prints "This object was initialized".
- - Pass to the initializer a parameter called name and store it in an instance variable.
- - The instance variable can be read and modified from outside the class.
- - Create a class method that is called "speaks" and prints "(name) says arf!"
- - Instantiate the class, pass it the parameter name, and use the speech method, the variable name, then modify the name and call it again by the new name.