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?

On Sat, Jan 14, 2012 at 4:14 PM, Brandon K. [email protected]
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.

Welcome to Ruby :slight_smile:

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

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

Brandon,

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

BK> First off I apologize for the ‘noobishness’ of this question. I’m on
a
BK> quest to teach myself programming and I started off with Ruby. Now
I’m
BK> following online lessons and it’s going pretty well but I’m stuck on
BK> program they suggested to make and I don’t want to move forward
until I
BK> know how to do it.

BK> They want me to write a program that asks for someone’s favorite
number
BK> then add 1 to the number and say that the program thinks that number
is
BK> just a little bit bigger and better. This is what I have:

BK> p ‘Hey you! What’s your favorite number?’
BK> fav = gets.chomp
BK> fav2 = fav.to_i + 1
BK> p ‘…well that’s an OK number but don’t you think ’ + fav2 + ’ is
a
BK> little bit bigger and better?’

BK> (I have puts abbreviated to p)

BK> 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

On Sat, Jan 14, 2012 at 4:54 PM, Brandon K. [email protected]
wrote:

really helped me out and I’m going to save it in case I need help in the
future!

You’re welcome.

Peter

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!

And Ralph, thank you for the extra reading!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs