Class: BoundedStack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lrjew/bounded_stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(list, maximum_size) ⇒ BoundedStack

Returns a new instance of BoundedStack.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
# File 'lib/lrjew/bounded_stack.rb', line 4

def initialize(list, maximum_size)
  @list = list
  @maximum_size = maximum_size
  @current_size = list.length
  raise ArgumentError unless @current_size <= @maximum_size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



29
30
31
# File 'lib/lrjew/bounded_stack.rb', line 29

def method_missing(method, *args, &block)
  @list.send(method, *args, &block)
end

Instance Method Details

#each(&block) ⇒ Object



33
34
35
# File 'lib/lrjew/bounded_stack.rb', line 33

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

#lengthObject Also known as: size



23
24
25
# File 'lib/lrjew/bounded_stack.rb', line 23

def length
  @current_size
end

#push(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lrjew/bounded_stack.rb', line 11

def push(data)
  shifted = nil
  if @current_size == @maximum_size
    shifted = @list.shift
  else
    @current_size += 1
  end

  node = @list.push(data)
  [node, shifted]
end