On 28/05/10 19:29, Ant Walliams wrote:
> Hi there,
>
> Sorry this is probably going to be a real easy question but I am totally
> new to programming.
>
> I was following some tutorial online and at the end of the first chapter
> it said 'Now create a program that asks the user for a number and then
> suggests a higher number as a better option.
>
> So what I wanted to happen below is it takes the users input and just
> adds 1 to it. But is is coming back with "rb:3:in '+' can't convert
> fixnum into string'
>
> puts 'What is your favourite number?'
> number = gets.chomp
> number+=1
> puts 'Do you not think' + number + 'is a better number?'
>
> Any help would be appreciated its doing my head in.
>
> Thanks
>    
When you call gets.chomp it returns a string (lets say it's "5"). That 
looks like a number to you and me but ruby treats it as text. You need 
to tell ruby to make it a number using .to_i.
ie The second line should read gets.chomp.to_i

Then ruby will be adding 5+1 and not "5"+1. I found that a good way to 
think of this was to replace "5" with a word or something. Could you do 
"hello"+1?

Angus