How to Print on a single line

Hi there,

How do I get the below script to print the 2nd part on a single line?

print 'Hello there, and what\'s your name? '
name = gets
puts 'Your name is ' + name + '? What a lovely name!'

Please see below, this is how it prints:

Hello there, and what's your name? Kareem
Your name is Kareem
? What a lovely name!

How do I get it to print like this:

Hello there, and what's your name? Kareem
Your name is Kareem? What a lovely name!

···

--
Posted via http://www.ruby-forum.com/.

name = gets.chomp

String#chomp removes the record separator (newline) at the end of the
string, if there is one.

http://ruby-doc.org/core-1.9.3/String.html#method-i-chomp

Kirk Haines

···

On Sun, Jul 8, 2012 at 1:18 PM, Kareem Adams <lists@ruby-forum.com> wrote:

Hi there,

How do I get the below script to print the 2nd part on a single line?

print 'Hello there, and what\'s your name? '
name = gets
puts 'Your name is ' + name + '? What a lovely name!'

Please see below, this is how it prints:

Hello there, and what's your name? Kareem
Your name is Kareem
? What a lovely name!

How do I get it to print like this:

Hello there, and what's your name? Kareem
Your name is Kareem? What a lovely name!'

Wow!

That works. Thanks a lot Kirk.

Just learning Rudy.

···

--
Posted via http://www.ruby-forum.com/.

`gets` returns string with separator (new line by default):
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-gets

So you can either strip the result (but it will remove any
leading/trailing whitespaces):

  name = gets.strip

or simply leave everything but the last char

  name = gets[0...-1]

···

On Mon, 9 Jul 2012 04:18:03 +0900 Kareem Adams <lists@ruby-forum.com> wrote:

print 'Hello there, and what\'s your name? '
name = gets
puts 'Your name is ' + name + '? What a lovely name!'

Please see below, this is how it prints:

Hello there, and what's your name? Kareem
Your name is Kareem
? What a lovely name!

How do I get it to print like this:

Hello there, and what's your name? Kareem
Your name is Kareem? What a lovely name!

--
Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: zapparov@jabber.ru

*Origin: Happy Hacking!

Hello,

···

On 8 Ιουλ 2012, at 22:18 , Kareem Adams wrote:

Hi there,

How do I get the below script to print the 2nd part on a single line?

print 'Hello there, and what\'s your name? '
name = gets
puts 'Your name is ' + name + '? What a lovely name!'

Your problem is on gets which needs to be turned into gets.chomp (as already addressed earlier).

There are several ways to print lines into ruby. If you are more accustomed to 'C' or if you need more flexibility you can print using printf and sprintf[1]. Also you can use just 'p'. Take a look at the example below:

-------
GreyJewel:~ atma$ irb
1.9.2p320 :001 > array = %w( a b c d e f g )
=> ["a", "b", "c", "d", "e", "f", "g"]
1.9.2p320 :002 > puts array
a
b
c
d
e
f
g
=> nil
1.9.2p320 :003 > p array
["a", "b", "c", "d", "e", "f", "g"]
=> ["a", "b", "c", "d", "e", "f", "g"]
1.9.2p320 :004 > printf("First letter:\t %s\n", array[0])
First letter: a
=> nil
1.9.2p320 :005 > quit
GreyJewel:~ atma$
--------------
Note that using printf I was able to add a tab using '\t' and a new line using '\n'. Take a look at [2] for more info.

Happy rubying :slight_smile:

[1] http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/function.html#sprintf
[2] http://en.wikipedia.org/wiki/Printf_format_string

Panagiotis Atmatzidis
-----------------------------
Pharmacy Student at VFU

email4lists: ml@convalesco.org
More info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."

Snap! That's what I forgot about :)) But even strange voice that was
telling me "man, `str[0...-1]` looks way too ugly" didn't forced me to
recheck manual :))

···

On Mon, 9 Jul 2012 04:26:15 +0900 Kirk Haines <wyhaines@gmail.com> wrote:

String#chomp removes the record separator (newline) at the end of the
string, if there is one.

--
Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: zapparov@jabber.ru

*Origin: Happy Hacking!