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
On Thu, Jun 25, 2009 at 7:20 PM, Mikem94590 [email protected] 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
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.
On Jun 25, 11:24 am, Ben L. [email protected] 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.
On Thu, Jun 25, 2009 at 7:32 PM, Ryan D.
[email protected]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
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