Join 307,096 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,029 people online right now. Registration is fast and FREE... Join Now!
Hey guys, I have been wanting to learn Ruby for a while but didn't see it going anywhere until yesterday when I saw a job posting locally for a Ruby programmer. So apparently it is going somewhere, right? So I downloaded RadRails and have been following a tutorial I found on the Ruby language website. I am having issues with one of the tasks it asked me to do though. I get an error when I try to gets a number from the user, and then add a number to it to display on a puts. Here is the code.
CODE
puts 'Hello there, and what\'s your name?' firstname = gets.chomp puts 'Your name is ' + firstname + '? What a lovely name!' puts 'Pleased to meet you, ' + firstname + '. ' puts 'Say, what is your middle name, ' + firstname + '?' middlename = gets.chomp puts 'Well ' + firstname + ' ' + middlename + ', what is your last name then?' lastname = gets.chomp wholename = firstname + ' ' + middlename + ' ' + lastname puts 'Alright. ' + wholename + ' it is! Glad to meet you.'
puts 'So, whats your favorite number ' + firstname + ' ?' usernum = gets.chomp puts 'Well, my favorite number is ' + (usernum.to_i + 1) + '. Which of course is far superior than yours.'
Its that last line that I am having errors on. Here is the error:
calc.rb:14:in `+': can't convert Fixnum into String (TypeError) from calc.rb:14
I have tried many different ways of writing it but I just don't get it.
Wow, I am finding Ruby a lot easier to learn than when I started with Java. The next test was to write out "99 Bottles of Beer" song.
CODE
bottles = 99 while bottles != 0 do puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottles of beer! Take one down pass it around, ' + (bottles.to_i - 1).to_s + ' bottles of beer on the wall!'; bottles -=1; end
Not too bad. But I do have a questions. Once the song get's down to only 1 bottle, how can I have it print "1 bottle of beer..." instead of "1 bottles of beer...". Because obviously that is grammatically incorrect.
This post has been edited by crummydo: 6 Oct, 2009 - 09:14 PM
bottles = 99 while bottles != 0 do puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottles of beer! Take one down pass it around, ' + (bottles.to_i - 1).to_s + ' bottles of beer on the wall!'; bottles -=1; if bottles = 1 puts '1 bottle of beer...' # add the rest here end end
Also, the loop doesn't terminate early enough, so the last line ends up having the bottles of beer. Annnnd in the line where you have 2, and go down to 1, it says 1 bottles.
How about:
CODE
bottles = 99 while bottles > 1 { puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottles of beer! Take one down pass it around, ' + (bottles -= 1).to_s + " bottle#{'s' unless bottles == 1} of beer on the wall!"; } puts '1 bottle of beer...' # add the rest here
This post has been edited by xclite: 7 Oct, 2009 - 08:14 AM
Ruby is a great programming language. It's quite popular, especially among web developers (I am not one), and I've been learning it for a while. I recommend you get The Pickaxe "Programming Ruby 1.9" book. It's great as a reference, and is awesome at teaching the language in a little bit of an unorthodox way.
This post has been edited by Raynes: 7 Oct, 2009 - 07:39 PM
Also, the loop doesn't terminate early enough, so the last line ends up having the bottles of beer. Annnnd in the line where you have 2, and go down to 1, it says 1 bottles.
Fixed it like this:
CODE
bottles = 99
while bottles > 2 do puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottles of beer! Take one down pass it around, ' + (bottles.to_i - 1).to_s + ' bottles of beer on the wall!'; bottles -=1; end puts '2 bottles of beer on the wall, 2 bottle of beer! Take one down, pass it around, 1 bottle of beer on the wall' puts '1 bottle of beer on the wall, 1 bottle of beer! Take one down, pass it around, no more bottles of beer on the wall'
I am sure there are "if" clauses that I could throw in there but I am still learning the ropes. At least it works as intended and is grammatically correct at the same time.
Also, the loop doesn't terminate early enough, so the last line ends up having the bottles of beer. Annnnd in the line where you have 2, and go down to 1, it says 1 bottles.
How about:
CODE
bottles = 99 while bottles > 1 { puts bottles.to_s + ' bottles of beer on the wall, ' + bottles.to_s + ' bottles of beer! Take one down pass it around, ' + (bottles -= 1).to_s + " bottle#{'s' unless bottles == 1} of beer on the wall!"; } puts '1 bottle of beer...' # add the rest here
ah thanks for fixing that for me, i knew it was something like that