I’ve created a Person class and my code is correct, but I just wanted someone to clarify a specific line.
class Person
attr_reader :first_name, :last_name, :age
def initialize (first_name, last_name, age)
@first_name = first_name
@last_name = last_name
@age = age
end
end
The line I’m confused about is the attr_reader one. Why does this need to be included and why do I need the : before each attribute?
attr_reader is a convenience method that ruby provides for adding the following method in an automated manner(getter methods). I.e. just one line of code will add these three methods in your case.
As for taking a symbol as input arguments, well that is how the method was defined. You can look a bit more at http://ruby-doc.org/core-1.8.7/Module.html#method-i-attr_reader