On Mar 8, 2010, at 7:11 PM, Mike Peltzer wrote:

> Hi all, I am very to new to ruby and am having trouble writing a very
> simple program: basically i just want to ask for an integer then  
> return
> the integer +1
> here is my code:
> ---------------------------------------------------
> puts 'what\'s your favorite number?'
> var1 = 1
> num = gets.chomp
> puts 'well ' + num.to_i + ' + var1 is better'
You're struggling with the difference between numbers and strings.  If  
you're used to awk or Perl where strings of digits behave like numbers  
when needed or numbers become strings of digits, this can seem odd.

Compare to this:

print "What's your favorite number? "
num = gets.chomp.to_i
puts "Well #{num + 1} is better."
puts 'Well ' + (num + 1).to_s + ' is better.'

The use of #{} in a double-quoted string is called interpolation and  
evaluates the contained expression and then calls .to_s on the result.

It is essentially what I've shown explicitly in the second line.

-Rob

>
> ------------------------
>
> when I run it, this is the error i get:
> ---------------------------------------
> what's your favorite number?
> 3
> ri20min.rb:5:in `+': can't convert Fixnum into String (TypeError)
>  from ri20min.rb:5:in `<main>'
> ---------------------------------------
> I realize this is laughable, but I am struggling with the concept, any
> help is greatly appreciated.
> -- 
> Posted via http://www.ruby-forum.com/.
>

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com