Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   split command (http://www.programmingforums.org/showthread.php?t=14044)

ggeo1 Sep 26th, 2007 10:57 AM

split command
 
Hello, i am a new ruby user and i started reading it the last days.
Generally,i know a few things from C++ .

I wrote a program which gives the amount of a radionuclide remaining after a period of time.I want to use the split command but somewhere i am making a mistake and i don't know where.I am giving you the code:

Quote:

#!/usr/bin/env ruby

puts " Give initial amount in mg,half life time in days and period of time at which to find amount remaining : "
data = gets.to_f
initial_amount,half_life,time = data.split(" ")

initial_amount = initial_amount.to_f
half_life = half_life.to_f
time = time.to_f

amount_remaining = initial_amount * (0.5) ** (time / half_life)
amount_remaining = amount_remaining.to_f

puts " The amount remaining in mg is = "
puts amount_remaining
Thanks

Arevos Sep 26th, 2007 11:49 AM

Quote:

Originally Posted by ggeo1 (Post 134333)
:

data = gets.to_f
initial_amount,half_life,time = data.split(" ")


You can only split a string, not a number. "gets" returns a string, but "to_f" turns it into a number (or to be more exact, a float). Get rid of the "to_f" for this line.

ggeo1 Sep 26th, 2007 12:03 PM

OK!

Thanks!

It worked! :)


All times are GMT -5. The time now is 3:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC