This is a follow up from Creating a class which inherits from another class in Ruby and m.one + m.two + m.three doesn’t work. We are applying for App Academy which is a Learn to Code course for people with little or no experience. As a result, these questions are similar, but I felt the answers in the other two posts did not address an answer, but diverged to explaining tangential concepts (which I did utilize)
To follow up on the first two mentioned links, I am having a problem with inherited classes. I have the Musician class which is inheriting three variables from the Person class. My problem is that when I run the
m.first_name + " " + m.last_name + ": " + m.age.to_s
I get an error: person.rb:31: undefined method `+’ for nil:NilClass (NoMethodError). I understand that this error has the answer, but I am not yet adept at understanding what this means.
One point to mention is that the query above, must be the way it is. I can not put Puts in front of it.
I would appreciate any suggestions as to why I am getting this statement regarding + being an undefined method.
Thanks!
edit: Thanks for the quick response. I didnt realize the coding community was so active! This is really amazing. I am going to edit the code to reflect the newest issues, so I dont keep getting the same suggestions.
It means that one of the three variables is
nil, i.e., it lacks a value:This is occurring because you are returning the result of calling the
putsfunction in all of your accessors. You need to return the variables themselves, not print them and return the result of the print function.Also, since you are already using
attr_readeryou havegetmethods created for you already. That’s the whole point of usingattr_reader; it creates a function which returns an underlying instance variable for you, you simply need to initialize it.for example, this:
is equivalent to