Ruby Output Problem

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

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

···

On Thu, Jun 25, 2009 at 7:20 PM, Mikem94590 <mike94590@gmail.com> wrote:

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

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

···

On Jun 25, 2009, at 11:24 , Ben Lovell wrote:

gets adds a newline so you should use chomp.

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

···

On Jun 25, 11:24 am, Ben Lovell <benjamin.lov...@gmail.com> wrote:

[Note: parts of this message were removed to make it a legal post.]

On Thu, Jun 25, 2009 at 7:20 PM, Mikem94590 <mike94...@gmail.com> wrote:
> 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

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

+1 point to you for passing my test :wink:

···

On Thu, Jun 25, 2009 at 7:32 PM, Ryan Davis <ryand-ruby@zenspider.com>wrote:

On Jun 25, 2009, at 11:24 , Ben Lovell wrote:

gets adds a newline so you should use chomp.

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

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

···

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/