Error - "`+': can't convert nil into String"

P

Paul Roche

Why does the code below give me this error? What can I do to fix it?

/people3.rb:4:in `+': can't convert nil into String (TypeError)
om C:/Ruby192/people3.rb:4:in `to_s'

class Person
attr_accessor :fname, :lname, :age
def to_s
"hi " + @fname + ", " + @lname
end

end



Thanks
PR
 
L

Luis Lavena

Why does the code below give me this error? What can I do to fix it?

/people3.rb:4:in `+': can't convert nil into String (TypeError)
om C:/Ruby192/people3.rb:4:in `to_s'

class Person
attr_accessor :fname, :lname, :age
def to_s
"hi " + @fname + ", " + @lname
end

end

Either @fname or @lname are nil

You can't concatenate nil to an string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top