Class: Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/clock.rb

Overview

Useage: ‘ruby -Ilib ./bin/clock 27,0` or `ruby -Ilib ./bin/clock 30,325`

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_minuteObject

queue to the minute track. Once the minute track is full the ball will drop to the five minute track triggering in reverse order the minute track to deposit to the main queue. When the five minute track is full it will pass the next ball from from the main queue to the hour track which triggers the five min track to drop its balls in reverse order to the main queue. When hour track is full will drop balls in reverse order to the main queue.



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
# File 'lib/clock.rb', line 20

def self.add_minute
  if @min_track.length < 4
    @min_track << @current_que.shift
  elsif @five_min_track.length < 11
    @five_min_track << @current_que.shift
    @current_que.concat(@min_track.reverse)
    @min_track = []
  elsif @hour_track.length < 11
    @hour_track << @current_que.shift
    @current_que.concat(@min_track.reverse)
    @min_track = []
    @current_que.concat(@five_min_track.reverse)
    @five_min_track = []
  else
    que_ball = @current_que.shift
    @current_que.concat(@min_track.reverse)
    @min_track = []
    @current_que.concat(@five_min_track.reverse)
    @five_min_track = []
    @current_que.concat(@hour_track.reverse)
    @current_que << que_ball
    @hour_track = []
    @hours_passed += 12
    @cycle_days = @hours_passed / 24
    if @current_que == @start_order
      @repeat = true
    end
  end
end

.run_ball_clock(balls, run_time) ⇒ Json

Returns json hash for min, five_min, hour and main queues. If no runtime is provided it will calculate the days tell the que repeats.

Parameters:

  • runs (Fixnum, Fixnum)

    clock with number of balls to start with and optionally a runtime.

Returns:

  • (Json)

    json hash for min, five_min, hour and main queues. If no runtime is provided it will calculate the days tell the que repeats.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/clock.rb', line 55

def self.run_ball_clock(balls, run_time)
  @current_que = []
  @hours_passed = '0'.to_i
  @repeat = false
  min_left = run_time.to_i
  @start_order = Array.new(balls.to_i) { |i| i + 1 }
  @start_order.freeze
  @min_track = []
  @five_min_track = []
  @hour_track = []
  que_balls = @start_order.clone
  que_balls.each do |ball|
    @current_que << ball
  end
  if run_time.to_i == 0 || nil
    until @repeat == true
      add_minute
    end
    result = puts "Clock cycles in: #{@cycle_days} day\(s\)\."
  else
    until min_left == 0
      add_minute
      min_left -= 1
    end
    json_clock = { min: @min_track, fiveMin: @five_min_track, hour: @hour_track, main: @current_que }
  end
  JSON.generate(json_clock)
end

Instance Method Details

#add_minute(addsaballfromthebeginingofthemain) ⇒ Object

queue to the minute track. Once the minute track is full the ball will drop to the five minute track triggering in reverse order the minute track to deposit to the main queue. When the five minute track is full it will pass the next ball from from the main queue to the hour track which triggers the five min track to drop its balls in reverse order to the main queue. When hour track is full will drop balls in reverse order to the main queue.



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
# File 'lib/clock.rb', line 20

def self.add_minute
  if @min_track.length < 4
    @min_track << @current_que.shift
  elsif @five_min_track.length < 11
    @five_min_track << @current_que.shift
    @current_que.concat(@min_track.reverse)
    @min_track = []
  elsif @hour_track.length < 11
    @hour_track << @current_que.shift
    @current_que.concat(@min_track.reverse)
    @min_track = []
    @current_que.concat(@five_min_track.reverse)
    @five_min_track = []
  else
    que_ball = @current_que.shift
    @current_que.concat(@min_track.reverse)
    @min_track = []
    @current_que.concat(@five_min_track.reverse)
    @five_min_track = []
    @current_que.concat(@hour_track.reverse)
    @current_que << que_ball
    @hour_track = []
    @hours_passed += 12
    @cycle_days = @hours_passed / 24
    if @current_que == @start_order
      @repeat = true
    end
  end
end