First off I apologize for the 'noobishness' of this question. I'm on a
quest to teach myself programming and I started off with Ruby.
Welcome to Ruby
Now I'm
following online lessons and it's going pretty well but I'm stuck on
program they suggested to make and I don't want to move forward until I
know how to do it.
They want me to write a program that asks for someone's favorite number
then add 1 to the number and say that the program thinks that number is
just a little bit bigger and better. This is what I have:
p 'Hey you! What\'s your favorite number?'
fav = gets.chomp
fav2 = fav.to_i + 1
p '...well that\'s an OK number but don\'t you think ' + fav2 + ' is a
little bit bigger and better?'
(I have puts abbreviated to p)
So far from what I've learned I'm stuck, what am I getting wrong here?
In your line
p '...well that\'s an OK number but don\'t you think ' + fav2 + ' is a
little bit bigger and better?'
You are "adding" 3 parts:
'...well that\'s an OK number but don\'t you think '
# this is a string (instance of String)
fav2
# this is a Fixnum (could also be Bignum is very big number)
' is a little bit bigger and better?'
# this is a string again
Now, the '+' operator cannot handle adding strings and numbers.
Either:
1) you add numbers:
puts 1 + 3 + 4 #=> 8
2) you add strings:
puts "I am" + " studying" + " ruby"
Now, you can convert numbers to strings with to_s
and you can convert strings to number with to_i (or to_f for floats).
So, this will work too:
puts "This is a nicer number:" + 36.to_s
puts 3 + "4".to_i + 5
I leave the exercise to you for your specific code.
HTH,
Peter
···
On Sat, Jan 14, 2012 at 4:14 PM, Brandon K. <brandon0981@gmail.com> wrote:
--
Peter Vandenabeele
http://twitter.com/peter_v
http://rails.vandenabeele.com