Forum: Ruby convert s to i, then + 1

Posted by Mike Peltzer (mspeltzer)
on 2010-03-09 01:11
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 by Rob Biedenharn (Guest)
on 2010-03-09 01:31
(Received via mailing list)
On Mar 8, 2010, at 7:11 PM, 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'
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

> 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
Posted by Kirk Haines (Guest)
on 2010-03-09 01:48
(Received via mailing list)
On Mon, Mar 8, 2010 at 5:11 PM, Mike Peltzer <mspeltzer@gmail.com> 
wrote:

>  from 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 = 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
Posted by Lui Kore (night_stalker)
on 2010-03-09 05:25
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 by Brian Candler (candlerb)
on 2010-03-09 10:54
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 by Marc Heiler (shevegen)
on 2010-03-09 13:02
Instead of:

  num = gets.chomp

you can directly do:

  num = gets.chomp.to_i
Posted by Prasanth Ravi (daretake)
on 2010-03-09 20:06
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 by Charlie Ca (artemisc360)
on 2010-03-11 19:12
Attachment: Numfav.rb (122 Bytes)
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.
Posted by Aldric Giacomoni (trevoke)
on 2010-03-11 19:29
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 by Josh Cheek (Guest)
on 2010-03-11 20:20
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.