From: Ant Walliams on
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
--
Posted via http://www.ruby-forum.com/.

From: Reid Thompson on
On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:
> Hi there,
> puts 'What is your favourite number?'
> number = gets.chomp
> number+=1
> puts 'Do you not think' + number + 'is a better number?'
puts 'Do you not think' + number.to_s + 'is a better number?'
>
> Any help would be appreciated its doing my head in.

From: Ant Walliams on
Reid Thompson wrote:
> On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:
>> Hi there,
>> puts 'What is your favourite number?'
>> number = gets.chomp
>> number+=1
>> puts 'Do you not think' + number + 'is a better number?'
> puts 'Do you not think' + number.to_s + 'is a better number?'

Thank for the reply but I am still getting the same error even after
making your changes. Any ideas?

Thanks
Ant
--
Posted via http://www.ruby-forum.com/.

From: Rob Biedenharn on
On May 28, 2010, at 2:59 PM, Ant Walliams wrote:
> Reid Thompson wrote:
>> On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:
>>> Hi there,
>>> puts 'What is your favourite number?'
>>> number = gets.chomp
>>> number+=1
>>> puts 'Do you not think' + number + 'is a better number?'
>> puts 'Do you not think' + number.to_s + 'is a better number?'
>
> Thank for the reply but I am still getting the same error even after
> making your changes. Any ideas?
>
> Thanks
> Ant

the problem is with number+=1

number is a string because gets.chomp is a string

number+=1 is syntactic sugar for
number = number + 1

And the expression number + 1 is trying to add 1 (a Fixnum) to a string

Try changing:

number = gets.chomp.to_i

to convert the string into an integer (which if small enough will be
held in an instance of Fixnum).

Then, you'll have the opposite problem with the last line unless you
incorporate Reid's suggestion.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob(a)AgileConsultingLLC.com



From: J-H Johansen on
On Fri, May 28, 2010 at 8:59 PM, Ant Walliams
<anthonywainwright(a)googlemail.com> wrote:
> Reid Thompson wrote:
>> On Sat, May 29, 2010 at 03:29:15AM +0900, Ant Walliams wrote:
>>> Hi there,
>>> puts 'What is your favourite number?'
>>> number = gets.chomp
>>> number+=1
>>> puts 'Do you not think' + number + 'is a better number?'
>>  puts 'Do you not think' + number.to_s + 'is a better number?'
>
> Thank for the reply but I am still getting the same error even after
> making your changes. Any ideas?
>
> Thanks
> Ant
> --
> Posted via http://www.ruby-forum.com/.
>
>

You need to convert the input from String to Fixnum

puts "What is your favourite number?"
number = gets.chomp.to_i
number+=1
puts "Do you not think #{number} is a better number?"


--
Jens-Harald Johansen
--
There are 10 kinds of people in the world: Those who understand binary and
those who don't...