A very very beginner question

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. 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?

···

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

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 :slight_smile:

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

Brandon,

Saturday, January 14, 2012, 8:14:32 AM, you wrote:

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. 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?

Try this

puts 'Hey you! What\'s your favorite number?'
fav = gets.chomp
puts "fav=" + fav
puts "fav.size=#{fav.size}"
fav2 = fav.to_i + 1
puts "fav2=" + fav2.to_s
puts '...well that\'s an OK number but don\'t you think ' + fav2.to_s + ' is a little bit bigger and better?'

and read these

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/217432
http://pine.fm/LearnToProgram/?Chapter=05

Thank you very much Peter, that cleared some things up for me, I
successively completed it :slight_smile:

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.to_s + '
is a little bit bigger and better?'

That's my final program, thanks again for a such a detailed answer, it
really helped me out and I'm going to save it in case I need help in the
future!

···

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

You're welcome.

Peter

···

On Sat, Jan 14, 2012 at 4:54 PM, Brandon K. <brandon0981@gmail.com> wrote:

Thank you very much Peter, that cleared some things up for me, I
successively completed it :slight_smile:

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.to_s + '
is a little bit bigger and better?'

That's my final program, thanks again for a such a detailed answer, it
really helped me out and I'm going to save it in case I need help in the
future!