Module: Stackable
Overview
Stackable mixin provides #pop, #push, #pull, etc. It depends on #slice, #splice and #insert.
Instance Method Summary collapse
-
#peek ⇒ Object
Peek at the top of the stack.
-
#poke(x) ⇒ Object
(also: #unshift)
Poke item onto the stack.
-
#pop ⇒ Object
Pop item off stack.
-
#pull ⇒ Object
(also: #shift)
Pull item off the stack.
-
#push(x) ⇒ Object
Push item onto the stack.
Instance Method Details
#peek ⇒ Object
Peek at the top of the stack.
a = [1, 2, 3]
a.peek #=> 3
a #=> [1, 2, 3]
82 83 84 |
# File 'lib/core/facets/stackable.rb', line 82 def peek splice(-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)?
70 71 72 |
# File 'lib/core/facets/stackable.rb', line 70 def poke(x) insert(0,x) end |
#pop ⇒ Object
Pop item off stack.
a = [1, 2, 3]
a.pop #=> 3
a #=> [1, 2]
38 39 40 |
# File 'lib/core/facets/stackable.rb', line 38 def pop splice(-1) end |
#pull ⇒ Object Also known as: shift
Pull item off the stack.
a = [1, 2, 3]
a.pull #=> 1
a #=> [2, 3]
57 58 59 |
# File 'lib/core/facets/stackable.rb', line 57 def pull slice(0) end |
#push(x) ⇒ Object
Push item onto the stack.
a = [1, 2]
a.push(3) #=> [1, 2, 3]
47 48 49 |
# File 'lib/core/facets/stackable.rb', line 47 def push(x) insert(-1,x) end |