Module: PostScript::Operators::Stack

Included in:
PostScript::Operators
Defined in:
lib/postscript/operators/stack.rb

Overview

A module containing all defined stack methods available to the PostScript runtime.

Instance Method Summary (collapse)

Instance Method Details

- (Object) copy

Adds a copy of the last n elements on the stack.



9
10
11
12
13
# File 'lib/postscript/operators/stack.rb', line 9

def copy
  n = pop

  push *stack.last(n)
end

- (Object) dup

Adds a duplicate copy of the top element to the end of the stack.



16
17
18
# File 'lib/postscript/operators/stack.rb', line 16

def dup
  push stack.last
end

- (Object) exch

Swaps the last two elements on the stack.



21
22
23
# File 'lib/postscript/operators/stack.rb', line 21

def exch
  push *(stack.pop(2).reverse)
end

- (Object) index

Adds a copy of the nth element on the stack.



26
27
28
# File 'lib/postscript/operators/stack.rb', line 26

def index
  push stack[-(pop + 1)]
end

- (Object) pop

Removes and returns the last element from the stack.

Returns:

  • the last element from the stack



33
34
35
# File 'lib/postscript/operators/stack.rb', line 33

def pop
  stack.pop
end

- (Object) roll

Moves the last n elements j positions on the stack, rolling over at stack boundaries.



39
40
41
42
43
# File 'lib/postscript/operators/stack.rb', line 39

def roll
  n, j = stack.pop(2)

  stack.push *stack.pop(n).rotate(-j)
end