Sha256: 3d210aa8e9824d8ced7d9cc4e0b79681e214b4c8e1316f596621cd88c6bf403e

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

require 'facets/indexable'
require 'facets/array/splice'

class Array

  include Indexable

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

  alias_method :first!, :shift

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

  alias_method :last!, :pop

  #
  #

  #alias_method :/, :[]

=begin
  # TODO Probably not best to overide store and fetch operators. Rename?

  # Modifies #[] to also accept an array of indexes.
  #
  #   a = ['a','b','c','d','e','f']
  #
  #   a[[1]]      #=> ['b']
  #   a[[1,1]]    #=> ['b','b']
  #   a[[1,-1]]   #=> ['b','f']
  #   a[[0,2,4]]  #=> ['a','c','e']
  #
  def [](*args)
    return values_at(*args.at(0)) if Array === args.at(0)
    return slice(*args)
  end

  # Modifies #[]= to accept an array of indexes for assignment.
  #
  #   a = ['a','b','c','d']
  #
  #   a[[1,-1]] = ['m','n']    #=> ['m','n']
  #   a                        #=> ['a','m','c','n']
  #
  def []=(*args)
    if Array === args.at(0)
      idx,vals = args.at(0),args.at(1)
      idx.each_with_index{ |a,i| store(a,vals.at(i)) }
      return values_at( *idx )
    else
      return store(*args)
    end
  end

end
=end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/array/indexable.rb
facets-2.8.3 lib/core/facets/array/indexable.rb
facets-2.8.2 lib/core/facets/array/indexable.rb
facets-2.8.1 lib/core/facets/array/indexable.rb
facets-2.8.0 lib/core/facets/array/indexable.rb
facets-2.7.0 lib/core/facets/array/indexable.rb
facets-2.6.0 lib/core/facets/array/indexable.rb
facets-2.5.1 lib/core/facets/array/indexable.rb
facets-2.5.0 lib/core/facets/array/indexable.rb
facets-2.5.2 lib/core/facets/array/indexable.rb