-
Notifications
You must be signed in to change notification settings - Fork 0
/
nums
executable file
·68 lines (50 loc) · 1.22 KB
/
nums
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
require 'optparse'
require 'time'
DEFAULT_LIMIT = 1 << 32
DEFAULT_SLEEP = 1
def nums(limit, options={})
sleep_time = options.fetch(:sleep, DEFAULT_SLEEP)
err_stream = STDERR
out_stream = STDOUT
limit.times do |i|
err_stream.puts("#{i} #{Time.now.strftime('@ %F %T %z')}")
err_stream.flush
out_stream.puts i
out_stream.flush
sleep sleep_time
end
end
def main
options={}
optparse = OptionParser.new do |opts|
opts.banner = <<-EOM
usage: #{File.basename($0)} [OPTIONS] [LIMIT]
Print integers up to LIMIT at regular intervals.
By default, sleep for #{DEFAULT_SLEEP} seconds between loop iterations, and
print integers up to 2**#{Math.log2(DEFAULT_LIMIT)}.
Options:
EOM
opts.on('-h', '--help', 'Display this message') do
puts opts
exit
end
opts.on('-s', '--sleep SEC', 'Sleep for SEC seconds between loops') do |v|
options[:sleep] = Float(v)
end
opts.on('-q', '--quiet', 'Be less verbose with output') do
options[:quiet] = true
end
end
optparse.parse!
limit = Integer(ARGV.fetch(0, DEFAULT_LIMIT))
begin
nums(limit, options)
rescue Interrupt
rescue Errno::EPIPE
exit 141
end
end
if $0 == __FILE__
main
end