Sha256: 1c650b65fa8b27a82b7bc42f5cea68e6cfab5d92e5fcb67a91b08a75526390d7

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

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

class Array
  include Indexable

  #--
  # Maybe not appropriate for indexable.rb, but where?
  #++
  alias_method :contains?, :include?

  #alias_method :/, :[]

  # 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

  # 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
class Array
  # 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-2.0.4 lib/core/facets/array/indexable.rb
facets-2.0.5 lib/core/facets/array/indexable.rb
facets-2.0.3 lib/core/facets/array/indexable.rb
facets-2.1.0 lib/core/facets/array/indexable.rb