Hey guys, great to be part of such a great community! I look forward to
learning Ruby alongside all of you!
I've very recently picked up ruby and have been reading a book called
"Learn to Program"(2005) by Chris Pine. Basically, it's a hands on guide
to Ruby. So I've been going through the basic programs with no problem,
up until now. I've been trying at a program for about an hour now, to no
prevail and was hoping you guys could lend me a hand.
The program asks the user to input their first, middle and last names in
separate strings and displays how many characters are in the combined
three names, in theory. I have no issues obtaining the string and
displaying them, but somewhere along the path of converting the string
to an integer I get messed up.
Now, I'll share some versions of this failed program that have yielded
errors (dashed line's indicate another program):
puts 'Enter your first, middle and last name:'
name1 = gets.chomp
name2 = gets.chomp
name3 = gets.chomp
names1 = name1.length
names2 = name2.length
names3 = name3.length
puts 'There are ' + names1.to_i + names2.to_i + names3.to_i
puts ' character\'s in your name!'
···
------------------------------------------
puts 'Enter your first, middle and last name:'
name1 = gets.chomp
name2 = gets.chomp
name3 = gets.chomp
puts 'There are ' + (name1.length).to_i + (name2.length).to_i +
(name3.length).to_i
puts ' character\'s in your name!'
-------------------------------------
And I also had another one (I must have accidently overwritten it) that
converted the strings to integers, by the ".length" method, and back to
strings and added them. Which yielded no error, but instead simply added
the string numbers together (5+6+4=564 instead of 5+6+4=15)
I was just wondering if somebody could take a look at this code and
point me in the right direction.
Much appreciated,
Bruce
--
Posted via http://www.ruby-forum.com/.