Sha256: b69e50548d223f1b73d62fc9468fa22bad804695da25542f9a2ae11778a0e4b8

Contents?: true

Size: 1.44 KB

Versions: 17

Compression:

Stored size: 1.44 KB

Contents

# = Stackable
#
# Stackable mixin provides #pop, #push, #pull, etc.
# It depends on #slice, #splice and #insert.
#
# == Authors
#
# * Thomas Sawyer
#
# == Copyright
#
# Copyright (c) 2007 Thomas Sawyer
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.


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

  # Pop item off stack.
  #
  #   a = [1, 2, 3]
  #   a.pop           #=> 3
  #   a               #=> [1, 2]

  def pop
    splice(-1)
  end

  # Push item onto the stack.
  #
  #   a = [1, 2]
  #   a.push(3)       #=> [1, 2, 3]

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

  # Pull item off the stack.
  #
  #   a = [1, 2, 3]
  #   a.pull          #=> 1
  #   a               #=> [2, 3]

  def pull
    slice(0)
  end

  alias_method :shift, :pull

  # Poke item onto the stack.
  #
  #   a = [2, 3]
  #   a.poke(1)       #=> [1, 2, 3]
  #
  #   TODO: Better name (besides unshift)?

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

  alias_method :unshift, :poke

  # Peek at the top of the stack.
  #
  #   a = [1, 2, 3]
  #   a.peek          #=> 3
  #   a               #=> [1, 2, 3]

  def peek
    slice(-1)
  end

end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
facets-2.8.4 lib/core/facets/stackable.rb
facets-2.8.3 lib/core/facets/stackable.rb
facets-2.8.2 lib/core/facets/stackable.rb
facets-2.8.1 lib/core/facets/stackable.rb
facets-2.8.0 lib/core/facets/stackable.rb
facets-2.7.0 lib/core/facets/stackable.rb
facets-2.6.0 lib/core/facets/stackable.rb
facets-2.4.0 lib/facets/stackable.rb
facets-2.4.1 lib/facets/stackable.rb
facets-2.4.4 lib/core/facets/stackable.rb
facets-2.4.3 lib/core/facets/stackable.rb
facets-2.4.2 lib/more/facets/stackable.rb
facets-2.5.1 lib/core/facets/stackable.rb
facets-2.4.5 lib/core/facets/stackable.rb
facets-2.5.0 lib/core/facets/stackable.rb
facets-2.5.2 lib/core/facets/stackable.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/stackable.rb