Sha256: 4d8ccbaa9c9c7272b218dce9c5590f62af424379a88689d792d6644b07896970

Contents?: true

Size: 1.32 KB

Versions: 22

Compression:

Stored size: 1.32 KB

Contents

require 'facet/array/last'

class Array

  # Change the first element.
  #
  #   a = ["a","y","z"]
  #   a.first = "x"
  #   p a           #=> ["x","y","z"]
  #
  def first=(x)
    self[0] = x
  end

  # Alias for shift, which removes and returns
  # the first element in an array.
  #
  #   a = ["a","y","z"]
  #   a.first!      #=> "a"
  #   p a           #=> ["y","z"]
  #
  alias_method( :first!, :shift)

  # Change the last element.
  #
  #   a = [1,2,5]
  #   a.last = 3
  #   p a           #=> [1,2,3]
  #
  def last=(x)
    self[-1] = x
  end

  # Alias for pop, which removes and returns
  # the last element in an array.
  #
  #   a = [1,2]
  #   a.last! 3
  #   p a           #=> [1,2,3]
  #
  alias_method( :last!, :pop)

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCArray < Test::Unit::TestCase

    def test_first_eq
      a = [1,2]
      a.first = 0
      assert_equal( [0,2], a )
    end

    def test_first!
      a = [1,2,3]
      assert_equal( 1, a.first! )
      assert_equal( [2,3], a )
    end

    def test_last_eq
      a = [1,2]
      a.last = 3
      assert_equal( [1,3], a )
    end

    def test_last!
      a = [1,2,3]
      assert_equal( 3, a.last! )
      assert_equal( [1,2], a )
    end

  end

=end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/array/first.rb
facets-1.0.0 lib/facet/array/first.rb
facets-1.2.1 lib/facets/core/array/first.rb
facets-1.2.0 lib/facets/core/array/first.rb
facets-1.3.0 lib/facets/core/array/first.rb
facets-1.1.0 lib/facet/array/first.rb
facets-1.3.3 lib/facets/core/array/first.rb
facets-1.3.1 lib/facets/core/array/first.rb
facets-1.3.2 lib/facets/core/array/first.rb
facets-1.4.2 lib/facets/core/array/first.rb
facets-1.4.0 lib/facets/core/array/first.rb
facets-1.4.1 lib/facets/core/array/first.rb
facets-1.4.3 lib/facets/core/array/first.rb
facets-1.4.4 lib/facets/core/array/first.rb
facets-1.4.5 lib/facets/core/array/first.rb
facets-1.7.38 lib/facets/core/array/first.rb
facets-1.7.0 lib/facets/core/array/first.rb
facets-1.7.30 lib/facets/core/array/first.rb
facets-1.7.46 lib/facets/core/array/first.rb
facets-1.8.20 lib/facets/core/array/first.rb