Class: Spectacular::SizedList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spectacular/sized_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ SizedList

Returns a new instance of SizedList.



5
6
7
8
# File 'lib/spectacular/sized_list.rb', line 5

def initialize size
  @size = size
  @list = []
end

Instance Method Details

#<<(item) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/spectacular/sized_list.rb', line 10

def << item
  x = @list << item
  while @list.size > @size
    @list.shift
  end
  x
end

#each(&block) ⇒ Object



18
19
20
# File 'lib/spectacular/sized_list.rb', line 18

def each &block
  @list.each(&block)
end

#last(n = nil) ⇒ Object



22
23
24
25
26
# File 'lib/spectacular/sized_list.rb', line 22

def last n = nil
  return @list.last unless n

  @list.last n
end

#lengthObject



28
29
30
# File 'lib/spectacular/sized_list.rb', line 28

def length
  @list.length
end