
class Array

  # Returns the middle element of an array, or the element offset
  # from middle if the parameter is given. Even-sized arrays,
  # not having an exact middle, return the middle-right element.
  #
  #   [1,2,3,4,5].mid        #=> 3
  #   [1,2,3,4,5,6].mid      #=> 4
  #   [1,2,3,4,5,6].mid(-1)  #=> 3
  #   [1,2,3,4,5,6].mid(-2)  #=> 2
  #   [1,2,3,4,5,6].mid(1)   #=> 5
  #
  # In other words, If there are an even number of elements the
  # higher-indexed of the two center elements is indexed as
  # orgin (0).
  def mid(offset=0)
    self.at( (self.length / 2) + offset )
  end

end



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

  require 'test/unit'

  class TCArray < Test::Unit::TestCase

    def test_mid
      a = [1,2,3,4,5]
      b = [1,2,3,4,5,6]
      assert_equal( 3, a.mid )
      assert_equal( 4, b.mid )
      assert_equal( 4, a.mid(1) )
      assert_equal( 5, b.mid(1) )
      assert_equal( 6, b.mid(2) )
      assert_equal( 3, b.mid(-1) )
    end

  end

=end
