Realy simple question I cant seem to find the answer to

Hi
I just wanted to write a simple conversion program in ruby but cant seem
to find what I need
This is an example script

puts "Enter the number miles"
miles = gets
puts "the number of km is " + miles // What I want to know is what I put
after the + miles to times it by 0.6 or whatever

PLEASE HELP

···

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

Hi
I just wanted to write a simple conversion program in ruby but cant seem
to find what I need
This is an example script

puts "Enter the number miles"
miles = gets
puts "the number of km is " + miles // What I want to know is what I put
after the + miles to times it by 0.6 or whatever

PLEASE HELP

It's a two step process.

1. You need to convert the +miles+ variable into a number. When you read from STDIN with #gets (which stands for "get string") it returns a string. You can multiply a string but that isn't what you really want. You want to multiply by the number the string represents. You get that representation by using the #to_f method on the string.

miles.to_f # will convert a string into a floating point number

2. Then you need to be able to append the value of (miles.to_f * 0.6) onto your text. You cannot append a number directly; you must append a string. So, you need to convert your number back into a string with #to_s.

(miles.to_f * 0.6).to_s

puts "the number of km is " + (miles.to_f * 0.6).to_s

Give that a try.

I highly recommend that you pick up a Ruby book or google around for a few tutorials.

cr

···

On Feb 1, 2012, at 3:10 PM, mark kirby wrote: