Creating an initializer method in a Ruby class
One thing you may find handy in Ruby development is to create an initializer method. If you're wondering what is an initializer method, it is simply a method called initialize
that will run every time when you create an instance of your class. In this method, you can give values to your variables, call other methods, and do just about anything that you think should happen when a new instance of that class is created.
If you're coming from other languages, this initializer method is similar to a constructor.
Adding an initializer to a Ruby class
Let's update our ApiConnector
class to utilize an initializer method.
This update class will look something like the following:
class ApiConnector def initialize(title, description, url) @title = title @description = description @url = url end end
For now, I'm creating an instance variable for each of my parameters so that I can use these variables in other parts of the application as well...