Convert s to i, then + 1

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'

------------------------

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

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

···

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

'well ' is an object that is an instance of the String class.

It has a method, +, that takes an argument which will be combined with
the value of the String object, returning a new instance of String. It
expects the argument to respond to #to_str.

num points to an object that is an instance of String.

When you call the #to_i method on it, you get back an object that is
an instance of Fixnum. Fixnum doesn't normally respond to #to_str, so
when it is passed as the argument to String#+, an exception is thrown.

Now, try this:

···

On Mon, Mar 8, 2010 at 5:11 PM, Mike Peltzer <mspeltzer@gmail.com> wrote:

puts 'what\'s your favorite number?'
var1 = 1
num = gets.chomp
puts 'well ' + num.to_i + ' + var1 is better'

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

-----
class Fixnum
  def to_str
    self.to_s
  end
end

puts 'what\'s your favorite number?'
var1 = 1
num = gets.chomp
puts 'well ' + num.to_i + ' + var1 is better'
-----

ruby /tmp/a.rb

what's your favorite number?
4
well 4 + var1 is better

That's not probably what you actually intend, but you can see that it
works, now.

If you actually want to add num and var, you probably want something like this:

puts 'well ' + (num.to_i + var1) + ' is better'

But you probably shouldn't do what I showed you in an actual program.
One of the powerful things about Ruby is that you can change core
classes if you need to, but you really should have a good reason, and
this isn't one. Better to just do this:

puts 'well ' + (num.to_i + var1).to_s + ' is better'

And, for that matter, this is even better:

puts "well #{num.to_i + var1} is better"

You see, variable interpolation in an instance of String calls to_s
for you on the result.

Hope that helps,

Kirk Haines

Or

num = gets.chomp
puts 'well ' + num.succ + ' + var1 is better'

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'

------------------------

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/\.

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'

------------------------

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.

puts 'what\'s your favorite number?'
puts "well #{gets.chomp.to_i + 1} is better"

same as the others suggested..

···

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

Kirk Haines wrote:

class Fixnum
  def to_str
    self.to_s
  end
end

Note: please treat this as for pedagogical purposes only. I'd say it's a
really bad idea to do this in real code. Firstly, you're changing a core
class, which may affect other libraries you use which depend on standard
behaviour. And secondly, making Fixnum auto-convert into a String may
mask bugs which then won't appear until later on in your program's
execution, making debugging difficult.

Better to be explicit with the conversions:

    preferred = num.to_i + var1
    puts 'well ' + preferred.to_s + ' is better'

or the more idiomatic use of string interpolation:

   puts "well #{num.to_i + var1} is better"

···

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

Instead of:

  num = gets.chomp

you can directly do:

  num = gets.chomp.to_i

···

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

this is the code I use;

puts 'Whats your favorite number?'
num = gets
puts (num.to_i + 1).to_s + ' is my favorite number. It one ups your
fav!'

Simple and Straightforward. I attached it to.

Attachments:
http://www.ruby-forum.com/attachment/4567/Numfav.rb

···

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

Charlie Ca wrote:

this is the code I use;

puts 'Whats your favorite number?'
num = gets
puts (num.to_i + 1).to_s + ' is my favorite number. It one ups your
fav!'

Simple and Straightforward. I attached it to.

puts "What is your favorite number?
num = gets.to_i = 1
puts "My favorite is #{num}. Hah! They should call you Mario 'cause
you've been one-upped!"

Alright, so I'm not showing anyone anything new, but I made a funny.

···

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

As long as their number isn't negative

print "Enter your favourite number: "
puts "My favourite is: #{gets.next}"

For decimals, it won't be 1 more, but rather the last digit will be
incremented.