-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
57 lines (46 loc) · 924 Bytes
/
test.rb
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
require_relative 'lib/shelley'
class Calculator
def initialize
@value = 0
end
def add(n)
@value += n.to_i
end
def subtract(n)
@value -= n.to_i
end
def multiply_by(n)
@value *= n.to_i
end
def divide_by(n)
@value /= n.to_i
end
def result
puts @value
end
end
class Timer
def initialize
@started_at = nil
@elapsed = 0
end
def start
return unless @started_at.nil?
@started_at = Time.now
end
def stop
return if @started_at.nil?
@elapsed += Time.now - @started_at
@started_at = nil
end
def elapsed
elapsed = @elapsed
elapsed += (Time.now - @started_at) unless @started_at.nil?
puts elapsed
end
end
command_registry = Shelley::CommandRegistry.new
command_registry.add_command(Calculator.new, 'calc')
command_registry.add_command(Timer.new, 'timer')
shell = Shelley::InteractiveShell.new(command_registry)
shell.start