Jump to content

Nautical bell: Difference between revisions

no edit summary
(Add Seed7 example)
No edit summary
Line 731:
</pre>
 
=={{header|Ruby}}==
<lang ruby>watches = [ "First", "Middle", "Morning", "Forenoon", "Afternoon", "First dog", "Last dog", "First" ]
watch_ends = [ "00:00", "04:00", "08:00", "12:00", "16:00", "18:00", "20:00", "23:59" ]
words = ["One","Two","Three","Four","Five","Six","Seven","Eight"]
sound = "ding!"
 
def watch_name hr_min
idx = watch_ends.find_index {|t| hr_min <= t}
watches[idx]
end
 
loop do
time = Time.now
if time.sec == 0 and time.min % 30 == 0
num = (time.hour * 60 + time.min) / 30 % 8
num = 8 if num == 0
hr_min = time.strftime "%H:%M"
text = "%s - %s watch, %s bell%s gone" % [
hr_min,
watch_name(hr_min),
words[num-1],
num==1 ? "" : "s"
]
bells = (sound * num).gsub(sound + sound) {|dd| dd + ' '}
puts "%-45s %s" % [text, bells]
end
sleep 1
end</lang>
{{out|Sample output}}
<pre>00:00 - First watch, Eight bells gone ding!ding! ding!ding! ding!ding! ding!ding!
00:30 - Middle watch, One bell gone ding!
01:00 - Middle watch, Two bells gone ding!ding!
...
17:30 - First dog watch, Three bells gone ding!ding! ding!
18:00 - First dog watch, Four bells gone ding!ding! ding!ding!
18:30 - Last dog watch, Five bells gone ding!ding! ding!ding! ding!
19:00 - Last dog watch, Six bells gone ding!ding! ding!ding! ding!ding!
...</pre>
 
=={{header|Seed7}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.