-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.rb
56 lines (43 loc) · 823 Bytes
/
sample.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
class Worm
include Enumerable
def initialize l
@max_lenght = l
@a = []
end
def each &block
@a.each &block
end
def << elem
@a.shift if @a.size == @max_lenght
@a << elem
return self
end
def push elem
@a << elem
@a.shift if @a.size > @max_lenght
end
end
class CappedContainer
include Enumerable
def initialize limit
# @limit = limit
@hash = Hash.new { |h,v| h[v] = Worm.new limit }
end
def << sample # wich is a Sample
a = @hash[sample.id]
a << sample
self
end
def push sample
a = @hash[sample.id]
a.push sample
end
def each &block
@hash.each do |id, ary|
ary.each do |sample|
yield sample
end
end
end
end # CappedContainer
Sample = Struct.new(:id, :data, :sample_id)