School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

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!




Trying to learn

 

Trying to learn

crummydo

6 Oct, 2009 - 07:31 PM
Post #1

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 190


My Contributions
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.



User is offlineProfile CardPM
+Quote Post


xclite

RE: Trying To Learn

6 Oct, 2009 - 08:05 PM
Post #2

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 655



Thanked: 14 times
My Contributions
Unlike Java, Ruby does not automatically call the to_s method. The arguments to puts must be either a string, or contained with the #{} operator:
CODE

puts "#{usernum + 1}"


is one option. Anything within the #{} is evaluated, and the result is printed. You could also do:

CODE

(usernum.to_i + 1).to_s


to accomplish the same thing.
User is online!Profile CardPM
+Quote Post

crummydo

RE: Trying To Learn

6 Oct, 2009 - 08:16 PM
Post #3

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 190


My Contributions
Tried your suggestions and they work. Thanks bud.
User is offlineProfile CardPM
+Quote Post

crummydo

RE: Trying To Learn

6 Oct, 2009 - 09:11 PM
Post #4

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 190


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Rickster0

RE: Trying To Learn

7 Oct, 2009 - 02:06 AM
Post #5

D.I.C Head
Group Icon

Joined: 8 Jul, 2008
Posts: 187



Thanked: 11 times
Dream Kudos: 150
My Contributions
try this

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;
if bottles = 1
puts '1 bottle of beer...' # add the rest here
end
end

User is offlineProfile CardPM
+Quote Post

xclite

RE: Trying To Learn

7 Oct, 2009 - 06:37 AM
Post #6

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 655



Thanked: 14 times
My Contributions
I get an infinite loop with that!
CODE

if bottles == 1

was probably the intended code.

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
User is online!Profile CardPM
+Quote Post

Raynes

RE: Trying To Learn

7 Oct, 2009 - 11:07 AM
Post #7

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
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
User is offlineProfile CardPM
+Quote Post

xclite

RE: Trying To Learn

7 Oct, 2009 - 04:31 PM
Post #8

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 655



Thanked: 14 times
My Contributions
Seconding Raynes' suggestion - however, he probably meant 1.9, not 1.6.
User is online!Profile CardPM
+Quote Post

crummydo

RE: Trying To Learn

7 Oct, 2009 - 05:20 PM
Post #9

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 190


My Contributions
QUOTE(xclite @ 7 Oct, 2009 - 06:37 AM) *

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.
User is offlineProfile CardPM
+Quote Post

Raynes

RE: Trying To Learn

7 Oct, 2009 - 07:40 PM
Post #10

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
QUOTE(xclite @ 7 Oct, 2009 - 04:31 PM) *

Seconding Raynes' suggestion - however, he probably meant 1.9, not 1.6.


Oops, sorry. Fixed.
User is offlineProfile CardPM
+Quote Post

Rickster0

RE: Trying To Learn

9 Oct, 2009 - 01:23 AM
Post #11

D.I.C Head
Group Icon

Joined: 8 Jul, 2008
Posts: 187



Thanked: 11 times
Dream Kudos: 150
My Contributions
QUOTE(xclite @ 7 Oct, 2009 - 06:37 AM) *

I get an infinite loop with that!
CODE

if bottles == 1

was probably the intended code.

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 smile.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 11:50AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month