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

**URL:** <https://rubytalk.org/t/error-cant-convert-nil-into-string/60247>\
**Category:** ruby-talk\
**Created:** [29 September 2010 03:03 UTC](https://rubytalk.org/t/error-cant-convert-nil-into-string/60247 "2010-09-29T03:03:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)\
**Post date:** [29 September 2010 03:03 UTC](https://rubytalk.org/t/error-cant-convert-nil-into-string/60247/1 "2010-09-29T03:03:22Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [29 September 2010 04:00 UTC](https://rubytalk.org/t/error-cant-convert-nil-into-string/60247/2 "2010-09-29T04:00:14Z")

</div>

Either @fname or @lname are nil

You can't concatenate nil to an string.

> **···**
>
> On Sep 29, 12:03 am, Paul Roche \<prpaulro...@gmail.com\> wrote:
> 
> > 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
> 
> --  
> Luis Lavena

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [29 September 2010 07:06 UTC](https://rubytalk.org/t/error-cant-convert-nil-into-string/60247/3 "2010-09-29T07:06:22Z")

</div>

@fname is nil, and you are trying to call the + method on it. Hence the error.  
The recommended way would be something like:

class Person  
&nbsp;&nbsp;attr\_accessor :fname, :lname, :age  
&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;"hi #{@fname}, #{@lname}"  
&nbsp;&nbsp;end  
end

irb(main):001:0\> class Person  
irb(main):002:1\> attr\_accessor :fname, :lname, :age  
irb(main):003:1\> def to\_s  
irb(main):004:2\> "hi #{@fname}, #{@lname}"  
irb(main):005:2\> end  
irb(main):006:1\>  
irb(main):007:1\* end  
=\> nil  
irb(main):008:0\> Person.new.to\_s  
=\> "hi , "  
irb(main):009:0\> p = Person.new  
=\> hi ,  
irb(main):010:0\> p.lname = "Smith"  
=\> "Smith"  
irb(main):011:0\> p.to\_s  
=\> "hi , Smith"  
irb(main):012:0\> p.fname = "John"  
=\> "John"  
irb(main):013:0\> p.to\_s  
=\> "hi John, Smith"

Jesus.

> **···**
>
> On Wed, Sep 29, 2010 at 5:03 AM, Paul Roche \<prpaulroche@gmail.com\> wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![Tomasz\_blazeroot](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Tomasz\_blazeroot](https://rubytalk.org/u/Tomasz_blazeroot)\
**Post date:** [29 September 2010 07:09 UTC](https://rubytalk.org/t/error-cant-convert-nil-into-string/60247/4 "2010-09-29T07:09:47Z")

</div>

> 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

Try:

"hi " + @fname.to\_s + ", " + @lname.to\_s"

Then nil would be an empty string.

> **···**
>
> --  
> Tomasz 'blazeroot'
