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]
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.