Sha256: 0b8137b91d607de5fe468fc223262b3a29e52b3f3af499b8188fa07698eab26f

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# = TITLE:
#
#   Stackable
#
# = DESCRIPTION:
#
#   Stackable mixin provides #pop, #push, #pull, etc.
#   It depends on #slice, #splice and #insert.
#
# = 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.
#
# = AUTHORS:
#
#   - Thomas Sawyer

# 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
    splice(-1)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.3.0 lib/core/facets/stackable.rb
facets-2.2.0 lib/core/facets/stackable.rb
facets-2.2.1 lib/core/facets/stackable.rb