# Ruby Output Problem

**URL:** <https://www.ruby-forum.com/t/ruby-output-problem/171050>\
**Category:** Ruby\
**Created:** [June 25, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050 "2009-06-25T20:21:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikem94590](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Mikem94590](https://www.ruby-forum.com/u/Mikem94590)\
**Post date:** [June 25, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050/1 "2009-06-25T20:21:13Z")

</div>

Hello all.  
I am new to Ruby, and am created a simple program that takes a name,  
and outputs "Hello " + name + “.”. My problem is that this is putting  
the output on multiple lines.

e.g: Hello Mike  
.

What am I doing wrong?

Below is the simple code I am using:

name = gets  
puts "Hello " + name + “.”  
sleep

---

<div class="post-metadata">

**Author:** ![Mikem94590](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Mikem94590](https://www.ruby-forum.com/u/Mikem94590)\
**Post date:** [June 25, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050/2 "2009-06-25T20:25:54Z")

</div>

On Thu, Jun 25, 2009 at 7:20 PM, Mikem94590 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Below is the simple code I am using:
> 
> name = gets  
> puts "Hello " + name + “.”  
> sleep

gets adds a newline so you should use chomp. Also, string interpolation  
is  
usually preferable to concatenation. This example is better:

name = gets.chomp  
puts “hello #{name}.”  
sleep

Ben

---

<div class="post-metadata">

**Author:** ![Mikem94590](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Mikem94590](https://www.ruby-forum.com/u/Mikem94590)\
**Post date:** [June 25, 2009, 8:32pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050/3 "2009-06-25T20:32:59Z")

</div>

On Jun 25, 2009, at 11:24 , Ben L. wrote:

> gets adds a newline so you should use chomp.

not quite. gets reads the newline that the user entered.

---

<div class="post-metadata">

**Author:** ![Mikem94590](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Mikem94590](https://www.ruby-forum.com/u/Mikem94590)\
**Post date:** [June 25, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050/4 "2009-06-25T20:35:55Z")

</div>

On Jun 25, 11:24 am, Ben L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > e.g: Hello Mike  
> > gets adds a newline so you should use chomp. Also, string interpolation is  
> > usually preferable to concatenation. This example is better:
> 
> name = gets.chomp  
> puts “hello #{name}.”  
> sleep
> 
> Ben

Thank you for you quick reply. Your advice worked, and seems like  
something that can be used for other purposes as well.

---

<div class="post-metadata">

**Author:** ![Mikem94590](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Mikem94590](https://www.ruby-forum.com/u/Mikem94590)\
**Post date:** [June 25, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050/5 "2009-06-25T21:35:10Z")

</div>

On Thu, Jun 25, 2009 at 7:32 PM, Ryan D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> On Jun 25, 2009, at 11:24 , Ben L. wrote:
> 
> gets adds a newline so you should use chomp.
> 
> > 
> 
> not quite. gets reads the newline that the user entered.

+1 point to you for passing my test 😉

---

<div class="post-metadata">

**Author:** ![Mikem94590](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@Mikem94590](https://www.ruby-forum.com/u/Mikem94590)\
**Post date:** [June 25, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/ruby-output-problem/171050/6 "2009-06-25T23:20:51Z")

</div>

On 25.06.2009 20:30, Mikem94590 wrote:

> Thank you for you quick reply. Your advice worked, and seems like  
> something that can be used for other purposes as well.

Another hint: if you want to see what’s going on often using “p” instead  
of “puts” helps because it will escape special chatercters. If applied  
to variable “name” after “gets” you would have seen the newline at the  
end:

$ ruby19 -e ‘n=gets;p n’  
hello  
“hello\n”

Kind regards

robert
