convert s to i, then + 1

M

Mike Peltzer

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

Rob Biedenharn

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

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
K

Kirk Haines

puts 'what\'s your favorite number?'
var1 =3D 1
num =3D 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)
=A0from ri20min.rb:5:in `<main>'

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

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

puts 'what\'s your favorite number?'
var1 =3D 1
num =3D 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 t=
his:

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
 
B

Brian Candler

Kirk said:
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"
 
P

Prasanth Ravi

Mike said:
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>'

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

same as the others suggested..
 
A

Aldric Giacomoni

Charlie said:
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.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top