'Ello. I was just wandering how you update the time in a ruby program
(like a clock). For instance:
COMP:WED Sep 30 18:51:30
COMP:WED Sep 30 18:51:31 #Replacing the above line
I don’t need the entire code for a clock (unless you happen to have it)
I just need to know the idea of how to make it. Thanks.
'Ello. I was just wandering how you update the time in a ruby program
(like a clock). For instance:
COMP:WED Sep 30 18:51:30
COMP:WED Sep 30 18:51:31 #Replacing the above line
I don’t need the entire code for a clock (unless you happen to have it)
I just need to know the idea of how to make it. Thanks.
To write to a terminal by replacing existing lines you need to send a
carriage-return without a line-feed (i.e. \r rather than \n on the end
of lines). If I’m being hacky, I generally just write them directly:
$stderr.write “\rstuff here”
Just be aware that anything longer than the new line will still be
visible (i.e.
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.
Am Donnerstag, 01. Okt 2009, 07:54:14 +0900 schrieb Scott Andrechek:
'Ello. I was just wandering how you update the time in a ruby program
(like a clock). For instance:
COMP:WED Sep 30 18:51:30
COMP:WED Sep 30 18:51:31 #Replacing the above line
You need a signal triggered every second so that you can refresh
the display. Just three weeks ago I posted this:
[
[“days”, (606024)],
[“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
This is what it outputs:
There are 43 days, 4 hours, 26 minutes, 28 seconds until Call of Duty 4:
Modern
Warfare 2 is released
There are 5 days, 3 hours, 26 minutes, 28 seconds until Operation
Flashpoint: Dr
agon Rising is released
There are 47 days, 4 hours, 26 minutes, 28 seconds until Assassin’s
Creed 2 is r
eleased
There are 151 days, 4 hours, 26 minutes, 28 seconds until God of War 3
is releas
ed
In that format. So besides the weird formatting it works great, thanks :
). If you do have time yes and I would like it explained as I’m fairly
new to Ruby and programming in general.