From: Logan Capaldo Date: 2006-05-11T09:22:59+09:00 Subject: Re: simple newbie question On May 10, 2006, at 8:13 PM, Bob Wickham wrote: > Hi, I'm just starting Ruby and theres something I'm sure is very > simple > but I just cant get it work. Its an exercise in Chris Pine's tutorial. > The question 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. > > My code: > puts 'What is your favourite number?' > number = gets.chomp > puts 'I think ' + (number.to_s + 1.to_s) + ' is a better number' > > is the only way I can get anything to work but this is adding > strings so > 2 + 1 becomes 21 which is not the objective. > > Of course, I need the answer to be 3. > > puts 'What is your favourite number?' > number = gets.chomp > puts 'I think ' + (number.to_i + 1.to_i) + ' is a better number' > > results in this error "cannot convert Fixnum into String (TypeError)" > > I've tried lots of other combinations but its not happening for me. > I know its simple but, hey, I have to start somewhere. > > Bob > > -- > Posted via http://www.ruby-forum.com/. > You can call methods on expressions... ( some code ).to_s