Topic: Hello all

What would be the best forum to post my very noobish questions on Ruby???

Thanks!

Re: Hello all

what do you need help with?

Re: Hello all

..probably the "Ruby Programming" board. So I moved the thread there wink

Re: Hello all

New to programming and Ruby so:

Working through this tutorial here:

http://pine.fm/LearnToProgram/?Chapter=04

It has, "Try these" extra stuff to do

The problem is:

Write a program which asks for a person's favorite number. Have your program add one to the number, then suggest the result as a bigger and better favorite number. (Do be tactful about it, though.)

The furthest I can get is this:

puts 'What is your favorite number?'

favoriteNumber = gets.chomp

puts 'Your favorite number is ' + favoriteNumber + '?'
suggestion = favoriteNumber

puts 'I think that ' + suggestion + ' is a better choice, don\'t you?'


I just can't get how to add a 1 to the favoriteNumber

Suggestion is there because I thought I could do suggestion = favoriteNumber + 1, nope

What am I just not "getting" here

Thanks

Last edited by Phil Hanson (2008-05-09 05:00:42)

Re: Hello all

suggestion = favoriteNumber.to_i + 1

Re: Hello all

Duplex wrote:

suggestion = favoriteNumber.to_i + 1

I actually thought of and tried that, Rubymate gives me

TypeError: can't convert Fixnum into String

method +    in getandchomp.rb at line 8
at top level    in getandchomp.rb at line 8
Program exited.

Last edited by Phil Hanson (2008-05-09 05:15:07)

Re: Hello all

Ah this works

puts 'What is your favorite number?'

favoriteNumber = gets.chomp

puts 'Your favorite number is ' + favoriteNumber + '?'

suggestion = favoriteNumber.to_i + 1

puts suggestion


Now to figure out how to put text with it

Re: Hello all

Sorry to clutter my own post but this worked.

puts 'What is your favorite number?'

favoriteNumber = gets.chomp

puts 'Your favorite number is ' + favoriteNumber + '?'

suggestion = favoriteNumber.to_i + 1

puts 'I think that ' + suggestion.to_s + ' is a better choice, don\'t you'


Is there a reason I had to convert it into a string?

Re: Hello all

you populated 'suggestion' with an integer and then you are appending it into a string which you are calling 'puts' on. You cannot append an integer to a string, so you convert the integer so it will play nice.

"Yeah, well, you know, that's just, like, your opinion, man." -El Duderino

http://jerodsanto.net