I am following the Codecademy Ruby course, 85% done.
Again and again, he asks you to create a class and pass some parameters and make them instance variables, for example:
class Computer def initialize(username, password) @username = username @password = password end end
Each time, it asks you to do the same instance variables as the parameters you passed.
This made me wonder if there is a Ruby way to handle this automatically, eliminating the need to type it all on my own every time.
I know what you can do
class Computer def initialize(username, password) @username, @password = username, password end end
but it almost doesn't print.
I did a few searches and found that you can create a set of "getters" using attr_reader , for example
class Song attr_reader :name, :artist, :duration end aSong = Song.new("Bicylops", "Fleck", 260) aSong.artist # "Fleck" aSong.name # "Bicylops" aSong.duration # 260
But as far as I can tell, this is not what I am looking for. I am not trying to automatically create getters and / or setters. What I'm looking for would be something like this
class Person def initialize(name, age, address, dob)
I was looking for a ruby shortcut to assign instance variables and so, but could not find the answer.
Is it possible? If so, how?