Just hacked all of this together. Probably full of bugs, haven't really tested it at all. Reply if you want me to explain it. 
targets = [
["Call of Duty 4: Modern Warfare 2", Time.mktime(2009,11,13)],
["Operation Flashpoint: Dragon Rising", Time.mktime(2009,10,6)],
["Assassin's Creed 2", Time.mktime(2009,11,17)],
["God of War 3", Time.mktime(2010,3)]
]
def time_in_words( target_time )
bucket = (target_time.to_i - Time.now.to_i).abs
[
["days", (60*60*24)],
["hours", (60*60)],
["minutes", (60)],
["seconds", (1)]
].collect { |interval|
plural,seconds = *interval
value = (bucket / seconds).to_i
bucket -= value * seconds
if value == 1
"#{value} #{plural[0...-1]}"
elsif value > 1
"#{value} #{plural}"
else
nil
end
}.compact.join(", ")
end
targets.each do |target|
title,date = *target
if date < Time.now
puts "#{title} came out #{time_in_words(date)} ago."
else
puts "There are #{time_in_words(date)} until #{title} is released"
end
end
Thomas.
···
On 1 Oct 2009, at 01:03, Scott Andrechek wrote:
I have tried all 3 tips and although i see how it would work maybe if i
be more specific it'll help you help me : ) . I am making a program
notify me how many days are left till an event happens (in the case of
my program when a game comes out). This is what I use:
puts 'Hey Scott : D'
time=Time.new
time_cod=Time.mktime(2009,11,13)
time_operationflash=Time.mktime(2009,10,6)
time_assassinscreed2=Time.mktime(2009,11,17)
time_godofwar3=Time.mktime(2010,3)
if (time_cod.to_i<0 or time_cod.to_i==0)
puts 'It\'s already out!'
else
puts 'There are ' + (((((time_cod-time)/60)/60)/24).to_i).to_s + '
days left until Call of Duty 4: Modern Warfare 2 is released'
end
if (time_operationflash.to_i<0 or time_operationflash.to_i==0)
puts 'It\'s already out!'
else
puts 'There are ' +
(((((time_operationflash-time)/60)/60)/24).to_i).to_s + ' days left
until Operation Flashpoint: Dragon Rising is released'
end
if (time_assassinscreed2.to_i<0 or time_assassinscreed2==0)
puts 'It\'s already out!'
else
puts 'There are ' +
(((((time_assassinscreed2-time)/60)/60)/24).to_i).to_s + ' days left
until Assassin\'s Creed 2 is released'
end
if (time_godofwar3.to_i<0 or time_godofwar3.to_i==0)
puts 'It\'s already out!'
else
puts 'There are ' + (((((time_godofwar3-time)/60)/60)/24).to_i).to_s
+ ' days left until God of War 3 is released'
end
sleep 50
I use sleep so I can start it without the command line. This does work
outputting:
Hey Scott : D
There are 43 days left until Call of Duty 4: Modern Warfare 2 is
released
There are 5 days left until Operation Flashpoint: Dragon Rising is
released
There are 47 days left until Assassin's Creed 2 is released
There are 151 days left until God of War 3 is released
I would like to change it that would say how many months, days, hours,
minutes, and seconds are left. Thanks again.
-Scott
--
Posted via http://www.ruby-forum.com/\.