Module: Stackable

Included in:
Array
Defined in:
lib/gems/facets-2.4.5/lib/core/facets/stackable.rb

Overview

Stackable

Stackable mixin provides #pop, #push, #pull, etc. It depends on #slice, #splice and #insert.

Instance Method Summary collapse

Instance Method Details

#peekObject

Peek at the top of the stack.

a = [1, 2, 3]
a.peek          #=> 3
a               #=> [1, 2, 3]


81
82
83
# File 'lib/gems/facets-2.4.5/lib/core/facets/stackable.rb', line 81

def peek
  slice(-1)
end

#poke(x) ⇒ Object Also known as: unshift

Poke item onto the stack.

a = [2, 3]
a.poke(1)       #=> [1, 2, 3]

TODO: Better name (besides unshift)?


69
70
71
# File 'lib/gems/facets-2.4.5/lib/core/facets/stackable.rb', line 69

def poke(x)
  insert(0,x)
end

#popObject

Pop item off stack.

a = [1, 2, 3]
a.pop           #=> 3
a               #=> [1, 2]


37
38
39
# File 'lib/gems/facets-2.4.5/lib/core/facets/stackable.rb', line 37

def pop
  splice(-1)
end

#pullObject Also known as: shift

Pull item off the stack.

a = [1, 2, 3]
a.pull          #=> 1
a               #=> [2, 3]


56
57
58
# File 'lib/gems/facets-2.4.5/lib/core/facets/stackable.rb', line 56

def pull
  slice(0)
end

#push(x) ⇒ Object

Push item onto the stack.

a = [1, 2]
a.push(3)       #=> [1, 2, 3]


46
47
48
# File 'lib/gems/facets-2.4.5/lib/core/facets/stackable.rb', line 46

def push(x)
  insert(-1,x)
end