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!

On Sun, Jul 8, 2012 at 1:18 PM, Kareem A. [email protected]
wrote:

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!’

name = gets.chomp

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

Kirk H.

Wow!

That works. Thanks a lot Kirk.

Just learning Rudy.

On Mon, 9 Jul 2012 04:18:03 +0900
Kareem A. [email protected] wrote:

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!

gets returns string with separator (new line by default):

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]


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!

On Mon, 9 Jul 2012 04:26:15 +0900
Kirk H. [email protected] wrote:

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

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 :))


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!

Hello,

On 8 Ιουλ 2012, at 22:18 , Kareem A. 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] printf - Wikipedia

Panagiotis A.