我目前正在苦学《算法导论》。里面的堆排序算法是用数组表示一棵二叉树,就像这样
                       A[1]
                   /         \
            A[2]              A[3]
          /     \            /   \
     A[4]       A[5]    A[6]    A[7]
    /   \
A[8]   A[9]       


正如您所看到的,要求数组的起始下标为1才方便。可是Ruby的数组的起始下标是0。我们的想法是为Ruby的Array增加一个属性“base_index”属性,用它来获取或设置数组的起始下标。先看一下完成后的效果吧:
让Ruby的数组支持任意起始下标= ['a','b','c','d','e']
让Ruby的数组支持任意起始下标a.base_index  
#=> 0
让Ruby的数组支持任意起始下标
a[0]  #=> 'a'
让Ruby的数组支持任意起始下标
a[13]  #=> ['b','c','d']
让Ruby的数组支持任意起始下标
a[1..4]  #=> ['b','c','d','e']
让Ruby的数组支持任意起始下标
a[-1]  #=> 'e'
让Ruby的数组支持任意起始下标

让Ruby的数组支持任意起始下标a.base_index 
= 1
让Ruby的数组支持任意起始下标a[
1]  #=> 'a'
让Ruby的数组支持任意起始下标
a[13]  #=> ['a','b','c']
让Ruby的数组支持任意起始下标
a[1..4]  #=> ['a','b','c','d']
让Ruby的数组支持任意起始下标
a[0]  #=> 'e'

本来以为只要重新定义Array的[]和[]=操作符就行了,后来发现原来有n多函数需要重新定义呀!全部的实现代码如下(文件名:“dynimic_base_index.rb”)
  1让Ruby的数组支持任意起始下标# Enhances Array to support any base index.
  2让Ruby的数组支持任意起始下标# It provides a property "base_index", indicates the current base index of the array object.
  3让Ruby的数组支持任意起始下标# The value of "base_index" influnces [] operator
  4让Ruby的数组支持任意起始下标#    a = ['a','b','c','d','e']
  5让Ruby的数组支持任意起始下标#    a.base_index  #=> 0
  6让Ruby的数组支持任意起始下标#    a[0]  #=> 'a'
  7让Ruby的数组支持任意起始下标#    a[1, 3]  #=> ['b','c','d']
  8让Ruby的数组支持任意起始下标#    a[1..4]  #=> ['b','c','d','e']
  9让Ruby的数组支持任意起始下标#    a[-1]  #=> 'e'
 10让Ruby的数组支持任意起始下标#
 11让Ruby的数组支持任意起始下标#    a.base_index = 1
 12让Ruby的数组支持任意起始下标#    a[1]  #=> 'a'
 13让Ruby的数组支持任意起始下标#    a[1, 3]  #=> ['a','b','c']
 14让Ruby的数组支持任意起始下标#    a[1..4]  #=> ['a','b','c','d']
 15让Ruby的数组支持任意起始下标#    a[0]  #=> 'e'
 16让Ruby的数组支持任意起始下标#
 17让Ruby的数组支持任意起始下标# and []= operator
 18让Ruby的数组支持任意起始下标#    a = Array.new
 19让Ruby的数组支持任意起始下标#    a.base_index = 1
 20让Ruby的数组支持任意起始下标#    a[4] = "4";                 #=> [nil, nil, nil, nil, "4"]
 21让Ruby的数组支持任意起始下标#    a[1, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
 22让Ruby的数组支持任意起始下标#    a[2..3] = [ 1, 2 ]          #=> ["a", 1, 2, nil, "4"]
 23让Ruby的数组支持任意起始下标#    a[1, 2] = "?"               #=> ["?", 2, nil, "4"]
 24让Ruby的数组支持任意起始下标#    a[1..3] = "A"               #=> ["A", "4"]
 25让Ruby的数组支持任意起始下标#    a[0]   = "Z"               #=> ["A", "Z"]
 26让Ruby的数组支持任意起始下标#    a[2..0] = nil              #=> ["A"]
 27让Ruby的数组支持任意起始下标# 
 28让Ruby的数组支持任意起始下标# and these functions:
 29让Ruby的数组支持任意起始下标#    at()
 30让Ruby的数组支持任意起始下标#    delete_at()
 31让Ruby的数组支持任意起始下标#    each_index()
 32让Ruby的数组支持任意起始下标#    fetch()
 33让Ruby的数组支持任意起始下标#    fill()
 34让Ruby的数组支持任意起始下标#    index()
 35让Ruby的数组支持任意起始下标#    insert()
 36让Ruby的数组支持任意起始下标#    rindex()
 37让Ruby的数组支持任意起始下标#    slice()
 38让Ruby的数组支持任意起始下标#    slice!()
 39让Ruby的数组支持任意起始下标#    values_at()
 40让Ruby的数组支持任意起始下标#    indexes()
 41让Ruby的数组支持任意起始下标#    indices()
 42让Ruby的数组支持任意起始下标class Array
 43让Ruby的数组支持任意起始下标  alias original_index_reader []
 44让Ruby的数组支持任意起始下标  alias original_index_writer []=
 45让Ruby的数组支持任意起始下标  alias original_at at
 46让Ruby的数组支持任意起始下标  alias original_delete_at delete_at
 47让Ruby的数组支持任意起始下标  alias original_each_index each_index
 48让Ruby的数组支持任意起始下标  alias original_fetch fetch
 49让Ruby的数组支持任意起始下标  alias original_fill fill
 50让Ruby的数组支持任意起始下标  alias original_index index
 51让Ruby的数组支持任意起始下标  alias original_insert insert
 52让Ruby的数组支持任意起始下标  alias original_rindex rindex
 53让Ruby的数组支持任意起始下标  alias original_slice slice
 54让Ruby的数组支持任意起始下标  alias original_slice! slice!
 55让Ruby的数组支持任意起始下标  alias original_values_at values_at
 56让Ruby的数组支持任意起始下标  alias original_indexes indexes
 57让Ruby的数组支持任意起始下标  alias original_indices indices
 58让Ruby的数组支持任意起始下标  
 59让Ruby的数组支持任意起始下标  def base_index()
 60让Ruby的数组支持任意起始下标    return defined?(@base_index)? @base_index : 0
 61让Ruby的数组支持任意起始下标  end
 62让Ruby的数组支持任意起始下标  
 63让Ruby的数组支持任意起始下标  def base_index=(value)
 64让Ruby的数组支持任意起始下标    @base_index = value
 65让Ruby的数组支持任意起始下标  end
 66让Ruby的数组支持任意起始下标  
 67让Ruby的数组支持任意起始下标  def at(index)
 68让Ruby的数组支持任意起始下标    return original_at(index - base_index)
 69让Ruby的数组支持任意起始下标  end
 70让Ruby的数组支持任意起始下标  
 71让Ruby的数组支持任意起始下标  def [](*args)
 72让Ruby的数组支持任意起始下标    if args.length == 1 && args.original_at(0).is_a?(Fixnum) then  # e.g. a[1]
 73让Ruby的数组支持任意起始下标      return original_at(args.original_at(0)-base_index)
 74让Ruby的数组支持任意起始下标    elsif args.length == 1 && args.original_at(0).is_a?(Range) then  # e.g. a[1..3]
 75让Ruby的数组支持任意起始下标      range = Range.new(args.original_at(0).begin-base_index, 
 76让Ruby的数组支持任意起始下标                                 args.original_at(0).end-base_index, 
 77让Ruby的数组支持任意起始下标                                 args.original_at(0).exclude_end?)
 78让Ruby的数组支持任意起始下标      return original_index_reader(range)
 79让Ruby的数组支持任意起始下标    elsif args.length == 2  then  #e.g. a[1, 2]
 80让Ruby的数组支持任意起始下标      return original_index_reader(args.original_at(0)-base_index, 
 81让Ruby的数组支持任意起始下标                                             args.original_at(1))
 82让Ruby的数组支持任意起始下标    else
 83让Ruby的数组支持任意起始下标      return original_index_reader(*args)
 84让Ruby的数组支持任意起始下标    end
 85让Ruby的数组支持任意起始下标  end
 86让Ruby的数组支持任意起始下标  
 87让Ruby的数组支持任意起始下标  def []=(*args)
 88让Ruby的数组支持任意起始下标    if args.length >= 2 then 
 89让Ruby的数组支持任意起始下标      if args.original_at(0).is_a?(Fixnum) then  # e.g. a[1]='Y' or a[1,3] = 'Z' or a[1] = ['a','b'] or a[1..3] = ['a','b']
 90让Ruby的数组支持任意起始下标        return original_index_writer(args.original_at(0)-base_index, 
 91让Ruby的数组支持任意起始下标                                              *args.original_index_reader(1..args.length-1))
 92让Ruby的数组支持任意起始下标      elsif args.original_at(0).is_a?(Range) then # e.g. a[1..3] = 'Y' or a[1..3] = ['Y','Z']
 93让Ruby的数组支持任意起始下标        range = Range.new(args.original_at(0).begin-base_index, 
 94让Ruby的数组支持任意起始下标                                   args.original_at(0).end-base_index, 
 95让Ruby的数组支持任意起始下标                                   args.original_at(0).exclude_end?)
 96让Ruby的数组支持任意起始下标        return original_index_writer(range, 
 97让Ruby的数组支持任意起始下标                                              *args.original_index_reader(1..args.length-1))
 98让Ruby的数组支持任意起始下标      end
 99让Ruby的数组支持任意起始下标    end
100让Ruby的数组支持任意起始下标  end
101让Ruby的数组支持任意起始下标  
102让Ruby的数组支持任意起始下标  def delete_at(index)
103让Ruby的数组支持任意起始下标    return original_delete_at(index - base_index)
104让Ruby的数组支持任意起始下标  end
105让Ruby的数组支持任意起始下标  
106让Ruby的数组支持任意起始下标  def each_index
107让Ruby的数组支持任意起始下标    (0让Ruby的数组支持任意起始下标self.length).each do |i|
108让Ruby的数组支持任意起始下标      yield(i+base_index)
109让Ruby的数组支持任意起始下标    end
110让Ruby的数组支持任意起始下标    
111让Ruby的数组支持任意起始下标    return self
112让Ruby的数组支持任意起始下标  end
113让Ruby的数组支持任意起始下标  
114让Ruby的数组支持任意起始下标  def fetch(*args)
115让Ruby的数组支持任意起始下标    if args.length == 1  # e.g. a.fetch(1)  or  a.fetch(1) { |value| value**2 }
116让Ruby的数组支持任意起始下标      if block_given?
117让Ruby的数组支持任意起始下标        return yield(self.original_at(args.original_at(0) - base_index))
118让Ruby的数组支持任意起始下标      else
119让Ruby的数组支持任意起始下标        return self.original_at(args.original_at(0) - base_index)
120让Ruby的数组支持任意起始下标      end
121让Ruby的数组支持任意起始下标    else  # e.g. a.fetch(5, 'cat')
122让Ruby的数组支持任意起始下标      return original_fetch(args.original_at(0)-base_index, 
123让Ruby的数组支持任意起始下标                                   *args.original_index_reader(1..args.length-1))
124让Ruby的数组支持任意起始下标    end
125让Ruby的数组支持任意起始下标  end
126让Ruby的数组支持任意起始下标  
127让Ruby的数组支持任意起始下标  def fill(*args)
128让Ruby的数组支持任意起始下标    if block_given? then
129让Ruby的数组支持任意起始下标      if args.length == 0 then  # e.g. a.fill {|i| i*i }
130让Ruby的数组支持任意起始下标        start_index = base_index
131让Ruby的数组支持任意起始下标        end_index = base_index + self.length - 1
132让Ruby的数组支持任意起始下标      elsif args.length == 1 && args.original_at(0).is_a?(Range)==false then  #e.g. a.fill(2) {|i| i*i }
133让Ruby的数组支持任意起始下标        start_index = args.original_at(0)
134让Ruby的数组支持任意起始下标        end_index = base_index + self.length - 1
135让Ruby的数组支持任意起始下标      elsif args.length == 1 && args.original_at(0).is_a?(Range) then  # e.g. a.fill(2..5) {|i| i*i }
136让Ruby的数组支持任意起始下标        start_index = args.original_at(0).begin
137让Ruby的数组支持任意起始下标        end_index = args.original_at(0).exclude_end?? args.original_at(0).end-1 : args.original_at(0).end
138让Ruby的数组支持任意起始下标      elsif args.length == 2 then  # e.g. a.fill(2,2) {|i| i*i }
139让Ruby的数组支持任意起始下标        start_index = args.original_at(0)
140让Ruby的数组支持任意起始下标        end_index = start_index + args.original_at(1- 1
141让Ruby的数组支持任意起始下标      else
142让Ruby的数组支持任意起始下标        original_fill(*args)  # original_fill will raise exception :)
143让Ruby的数组支持任意起始下标      end
144让Ruby的数组支持任意起始下标      (start_index..end_index).each do |i|
145让Ruby的数组支持任意起始下标        self[i] = yield(i)
146让Ruby的数组支持任意起始下标      end
147让Ruby的数组支持任意起始下标    else 
148让Ruby的数组支持任意起始下标      if args.length == 1  # e.g. a.fill('x') 
149让Ruby的数组支持任意起始下标        obj = args.original_at(0)
150让Ruby的数组支持任意起始下标        start_index = base_index
151让Ruby的数组支持任意起始下标        end_index = base_index + self.length - 1
152让Ruby的数组支持任意起始下标      elsif args.length == 2 && args.original_at(1).is_a?(Range)==false  # e.g. a.fill('x', 2)
153让Ruby的数组支持任意起始下标        obj = args.original_at(0)
154让Ruby的数组支持任意起始下标        start_index = args.original_at(1)
155让Ruby的数组支持任意起始下标        end_index = base_index + self.length - 1
156让Ruby的数组支持任意起始下标      elsif args.length == 2 && args.original_at(1).is_a?(Range)  # e.g. a.fill('x', 2..5)
157让Ruby的数组支持任意起始下标        obj = args.original_at(0)
158让Ruby的数组支持任意起始下标        start_index = args.original_at(1).begin
159让Ruby的数组支持任意起始下标        end_index = args.original_at(1).exclude_end?? args.original_at(1).end-1 : args.original_at(1).end
160让Ruby的数组支持任意起始下标      elsif args.length == 3 # e.g. a.fill('x', 2, 2)
161让Ruby的数组支持任意起始下标        obj = args.original_at(0)
162让Ruby的数组支持任意起始下标        start_index = args.original_at(1)
163让Ruby的数组支持任意起始下标        end_index = start_index + args.original_at(2- 1
164让Ruby的数组支持任意起始下标      else
165让Ruby的数组支持任意起始下标        original_fill(*args)  # original_fill will raise exception :)
166让Ruby的数组支持任意起始下标      end
167让Ruby的数组支持任意起始下标      original_fill(obj, Range.new(start_index-base_index, end_index-base_index, false))
168让Ruby的数组支持任意起始下标    end
169让Ruby的数组支持任意起始下标      
170让Ruby的数组支持任意起始下标    return self
171让Ruby的数组支持任意起始下标  end
172让Ruby的数组支持任意起始下标  
173让Ruby的数组支持任意起始下标  def index(value)
174让Ruby的数组支持任意起始下标    result = original_index(value) 
175让Ruby的数组支持任意起始下标    return result && (result + base_index)
176让Ruby的数组支持任意起始下标  end
177让Ruby的数组支持任意起始下标  
178让Ruby的数组支持任意起始下标  def indexes(*args)
179让Ruby的数组支持任意起始下标    arguments = Array.new
180让Ruby的数组支持任意起始下标    
181让Ruby的数组支持任意起始下标    args.each do |arg|
182让Ruby的数组支持任意起始下标      if arg.is_a?(Range)
183让Ruby的数组支持任意起始下标        range = Range.new(arg.begin-base_index, 
184让Ruby的数组支持任意起始下标                                   arg.end-base_index, 
185让Ruby的数组支持任意起始下标                                   arg.exclude_end?)
186让Ruby的数组支持任意起始下标        arguments << range
187让Ruby的数组支持任意起始下标      else
188让Ruby的数组支持任意起始下标        arguments << arg-base_index
189让Ruby的数组支持任意起始下标      end
190让Ruby的数组支持任意起始下标    end
191让Ruby的数组支持任意起始下标    
192让Ruby的数组支持任意起始下标    return original_indexes(*arguments)
193让Ruby的数组支持任意起始下标  end
194让Ruby的数组支持任意起始下标  
195让Ruby的数组支持任意起始下标  def indices(*args)
196让Ruby的数组支持任意起始下标    arguments = Array.new
197让Ruby的数组支持任意起始下标    
198让Ruby的数组支持任意起始下标    args.each do |arg|
199让Ruby的数组支持任意起始下标      if arg.is_a?(Range)
200让Ruby的数组支持任意起始下标        range = Range.new(arg.begin-base_index, 
201让Ruby的数组支持任意起始下标                                   arg.end-base_index, 
202让Ruby的数组支持任意起始下标                                   arg.exclude_end?)
203让Ruby的数组支持任意起始下标        arguments << range
204让Ruby的数组支持任意起始下标      else
205让Ruby的数组支持任意起始下标        arguments << arg-base_index
206让Ruby的数组支持任意起始下标      end
207让Ruby的数组支持任意起始下标    end
208让Ruby的数组支持任意起始下标    
209让Ruby的数组支持任意起始下标    return original_indices(*arguments)
210让Ruby的数组支持任意起始下标  end
211让Ruby的数组支持任意起始下标  
212让Ruby的数组支持任意起始下标  def insert(*args)
213让Ruby的数组支持任意起始下标    if args.length >= 1 
214让Ruby的数组支持任意起始下标      original_insert(args.original_at(0)-base_index, 
215让Ruby的数组支持任意起始下标                         *args.original_index_reader(1..args.length-1))
216让Ruby的数组支持任意起始下标    else
217让Ruby的数组支持任意起始下标      original_insert(*args)  # original_insert will raise exception :)
218让Ruby的数组支持任意起始下标    end    
219让Ruby的数组支持任意起始下标  end
220让Ruby的数组支持任意起始下标  
221让Ruby的数组支持任意起始下标  def rindex(value)
222让Ruby的数组支持任意起始下标    result = original_rindex(value)
223让Ruby的数组支持任意起始下标    return result && (result + base_index)
224让Ruby的数组支持任意起始下标  end
225让Ruby的数组支持任意起始下标  
226让Ruby的数组支持任意起始下标  def slice(*args)
227让Ruby的数组支持任意起始下标    return self[*args]
228让Ruby的数组支持任意起始下标  end
229让Ruby的数组支持任意起始下标  
230让Ruby的数组支持任意起始下标  def slice!(*args)
231让Ruby的数组支持任意起始下标    result = self[*args]
232让Ruby的数组支持任意起始下标    delete_at(*args)
233让Ruby的数组支持任意起始下标    return result
234让Ruby的数组支持任意起始下标  end
235让Ruby的数组支持任意起始下标  
236让Ruby的数组支持任意起始下标  def values_at(*args)
237让Ruby的数组支持任意起始下标    arguments = Array.new
238让Ruby的数组支持任意起始下标    
239让Ruby的数组支持任意起始下标    args.each do |arg|
240让Ruby的数组支持任意起始下标      if arg.is_a?(Range)
241让Ruby的数组支持任意起始下标        range = Range.new(arg.begin-base_index, 
242让Ruby的数组支持任意起始下标                                   arg.end-base_index, 
243让Ruby的数组支持任意起始下标                                   arg.exclude_end?)
244让Ruby的数组支持任意起始下标        arguments << range
245让Ruby的数组支持任意起始下标      else
246让Ruby的数组支持任意起始下标        arguments << arg-base_index
247让Ruby的数组支持任意起始下标      end
248让Ruby的数组支持任意起始下标    end
249让Ruby的数组支持任意起始下标    
250让Ruby的数组支持任意起始下标    return original_values_at(*arguments)
251让Ruby的数组支持任意起始下标  end
252让Ruby的数组支持任意起始下标end

单元测试代码(文件名“TestDynimicBaseIndex.rb”)
  1让Ruby的数组支持任意起始下标require 'dynimic_base_index'
  2让Ruby的数组支持任意起始下标require 'test/unit'
  3让Ruby的数组支持任意起始下标
  4让Ruby的数组支持任意起始下标class TestDynimicBaseIndex < Test::Unit::TestCase
  5让Ruby的数组支持任意起始下标  def test_base_index
  6让Ruby的数组支持任意起始下标     a = ['a','b','c','d','e']
  7让Ruby的数组支持任意起始下标     assert_equal(0, a.base_index)
  8让Ruby的数组支持任意起始下标     a.base_index = 1
  9让Ruby的数组支持任意起始下标     assert_equal(1, a.base_index)
 10让Ruby的数组支持任意起始下标   end
 11让Ruby的数组支持任意起始下标   
 12让Ruby的数组支持任意起始下标   def test_index_reader_operator()
 13让Ruby的数组支持任意起始下标       a = ['a','b','c','d','e']
 14让Ruby的数组支持任意起始下标       
 15让Ruby的数组支持任意起始下标       assert_equal(0, a.base_index)
 16让Ruby的数组支持任意起始下标       assert_equal('a', a[0])
 17让Ruby的数组支持任意起始下标       assert_equal(['b','c','d'], a[1,3])
 18让Ruby的数组支持任意起始下标       assert_equal(['b','c','d','e'], a[1..4])
 19让Ruby的数组支持任意起始下标       assert_equal('e', a[-1])
 20让Ruby的数组支持任意起始下标       
 21让Ruby的数组支持任意起始下标       a.base_index = 1
 22让Ruby的数组支持任意起始下标       assert_equal(1, a.base_index)
 23让Ruby的数组支持任意起始下标       assert_equal('a', a[1])
 24让Ruby的数组支持任意起始下标       assert_equal(['a','b','c'], a[1,3])
 25让Ruby的数组支持任意起始下标       assert_equal(['a','b','c','d'], a[1..4])
 26让Ruby的数组支持任意起始下标       assert_equal('e', a[0])
 27让Ruby的数组支持任意起始下标       assert_equal(nil, a[6])
 28让Ruby的数组支持任意起始下标       assert_equal([], a[6,1])
 29让Ruby的数组支持任意起始下标       assert_equal([], a[6..10])
 30让Ruby的数组支持任意起始下标       assert_equal(nil, a[7,1])
 31让Ruby的数组支持任意起始下标     end
 32让Ruby的数组支持任意起始下标     
 33让Ruby的数组支持任意起始下标     def test_index_writer_operator()
 34让Ruby的数组支持任意起始下标       a = ['a','b','c','d','e']
 35让Ruby的数组支持任意起始下标       a.base_index = 1
 36让Ruby的数组支持任意起始下标       a[1= 'Y'
 37让Ruby的数组支持任意起始下标       assert_equal(['Y','b','c','d','e'], a)
 38让Ruby的数组支持任意起始下标       
 39让Ruby的数组支持任意起始下标       b = ['a','b','c','d','e']
 40让Ruby的数组支持任意起始下标       b.base_index = 1
 41让Ruby的数组支持任意起始下标       b[1,3= ['Y','Z']
 42让Ruby的数组支持任意起始下标       assert_equal(['Y','Z','d','e'], b)
 43让Ruby的数组支持任意起始下标       
 44让Ruby的数组支持任意起始下标       c = ['a','b','c','d','e']
 45让Ruby的数组支持任意起始下标       c.base_index = 1
 46让Ruby的数组支持任意起始下标       c[1,3= 'Y'
 47让Ruby的数组支持任意起始下标       assert_equal(['Y','d','e'], c)
 48让Ruby的数组支持任意起始下标       
 49让Ruby的数组支持任意起始下标       d = ['a','b','c','d','e']
 50让Ruby的数组支持任意起始下标       d.base_index = 1
 51让Ruby的数组支持任意起始下标       d[1..3= 'Y'
 52让Ruby的数组支持任意起始下标       assert_equal(['Y','d','e'], d)
 53让Ruby的数组支持任意起始下标       
 54让Ruby的数组支持任意起始下标       e = ['a','b','c','d','e']
 55让Ruby的数组支持任意起始下标       e.base_index = 1
 56让Ruby的数组支持任意起始下标       e[1..3= ['Y''Z']
 57让Ruby的数组支持任意起始下标       assert_equal(['Y','Z','d','e'], e)
 58让Ruby的数组支持任意起始下标     end
 59让Ruby的数组支持任意起始下标     
 60让Ruby的数组支持任意起始下标     def test_at()
 61让Ruby的数组支持任意起始下标       a = ['a','b','c','d','e']
 62让Ruby的数组支持任意起始下标       assert_equal('a', a.at(0))
 63让Ruby的数组支持任意起始下标       a.base_index = 1
 64让Ruby的数组支持任意起始下标       assert_equal('a', a.at(1))
 65让Ruby的数组支持任意起始下标     end
 66让Ruby的数组支持任意起始下标     
 67让Ruby的数组支持任意起始下标     def test_delete_at()
 68让Ruby的数组支持任意起始下标       a = ['a','b','c','d','e']
 69让Ruby的数组支持任意起始下标       a.base_index = 1
 70让Ruby的数组支持任意起始下标       assert_equal('a', a.delete_at(1))
 71让Ruby的数组支持任意起始下标       assert_equal(['b','c','d','e'], a)
 72让Ruby的数组支持任意起始下标     end
 73让Ruby的数组支持任意起始下标     
 74让Ruby的数组支持任意起始下标     def test_each_index()
 75让Ruby的数组支持任意起始下标       a = ['a','b','c','d','e']
 76让Ruby的数组支持任意起始下标       expected_indexes = [1,2,3,4,5]
 77让Ruby的数组支持任意起始下标       actual_indexes = Array.new
 78让Ruby的数组支持任意起始下标       
 79让Ruby的数组支持任意起始下标       a.base_index = 1
 80让Ruby的数组支持任意起始下标       
 81让Ruby的数组支持任意起始下标       a.each_index do |i|
 82让Ruby的数组支持任意起始下标         actual_indexes << i
 83让Ruby的数组支持任意起始下标       end
 84让Ruby的数组支持任意起始下标       
 85让Ruby的数组支持任意起始下标       assert_equal(expected_indexes, actual_indexes)
 86让Ruby的数组支持任意起始下标     end
 87让Ruby的数组支持任意起始下标     
 88让Ruby的数组支持任意起始下标     def test_fetch()
 89让Ruby的数组支持任意起始下标       a = [ 11223344 ]
 90让Ruby的数组支持任意起始下标       a.base_index = 1
 91让Ruby的数组支持任意起始下标       
 92让Ruby的数组支持任意起始下标       assert_equal(11, a.fetch(1))
 93让Ruby的数组支持任意起始下标       assert_equal(44, a.fetch(0))
 94让Ruby的数组支持任意起始下标       assert_equal(121, a.fetch(1) { |value| value**2 } )
 95让Ruby的数组支持任意起始下标       
 96让Ruby的数组支持任意起始下标       assert_equal('cat', a.fetch(5'cat'))
 97让Ruby的数组支持任意起始下标     end
 98让Ruby的数组支持任意起始下标     
 99让Ruby的数组支持任意起始下标     def test_fill()
100让Ruby的数组支持任意起始下标       a = [ "a""b""c""d" ]
101让Ruby的数组支持任意起始下标       a.base_index = 1
102让Ruby的数组支持任意起始下标       
103让Ruby的数组支持任意起始下标       assert_equal(["x","x","x","x"], a.fill("x"))
104让Ruby的数组支持任意起始下标       assert_equal(["x","y","y","y"], a.fill("y"2))
105让Ruby的数组支持任意起始下标       assert_equal(["x","z","z","y"], a.fill("z"22))
106让Ruby的数组支持任意起始下标       assert_equal(["x","a","a","a"], a.fill("a"2..4))
107让Ruby的数组支持任意起始下标       
108让Ruby的数组支持任意起始下标       a = [ "a""b""c""d" ]
109让Ruby的数组支持任意起始下标       a.base_index = 1
110让Ruby的数组支持任意起始下标       assert_equal([1,4,9,16], a.fill { |i| i*i })
111让Ruby的数组支持任意起始下标       assert_equal([1,8,18,32], a.fill(2) { |i| a[i]*2 })
112让Ruby的数组支持任意起始下标       assert_equal([1,4,9,32], a.fill(2,2) { |i| a[i]/2 })
113让Ruby的数组支持任意起始下标       assert_equal([1,4,6,8], a.fill(2..4) { |i| i*2 })       
114让Ruby的数组支持任意起始下标     end
115让Ruby的数组支持任意起始下标     
116让Ruby的数组支持任意起始下标     def test_index()
117让Ruby的数组支持任意起始下标       a = [ "a""b""c""d" ]
118让Ruby的数组支持任意起始下标       a.base_index = 1
119让Ruby的数组支持任意起始下标       assert_equal(2, a.index('b'))
120让Ruby的数组支持任意起始下标       assert_equal(nil, a.index('x'))
121让Ruby的数组支持任意起始下标     end
122让Ruby的数组支持任意起始下标     
123让Ruby的数组支持任意起始下标     def test_insert()
124让Ruby的数组支持任意起始下标       a = [11,22,33]
125让Ruby的数组支持任意起始下标       a.base_index = 1
126让Ruby的数组支持任意起始下标       assert_equal([11,22,33], a.insert(2))
127让Ruby的数组支持任意起始下标       assert_equal([11,'a',22,33], a.insert(2'a'))
128让Ruby的数组支持任意起始下标       assert_equal([11,['!','@'],'a',22,33], a.insert(2, ['!','@']))
129让Ruby的数组支持任意起始下标       assert_equal([11,'Q','S',['!','@'],'a',22,33], a.insert(2'Q','S'))
130让Ruby的数组支持任意起始下标     end
131让Ruby的数组支持任意起始下标     
132让Ruby的数组支持任意起始下标     def test_rindex()
133让Ruby的数组支持任意起始下标       a = [ "a""b""b""b""c" ]
134让Ruby的数组支持任意起始下标       a.base_index = 1
135让Ruby的数组支持任意起始下标       
136让Ruby的数组支持任意起始下标       assert_equal(4, a.rindex("b"))
137让Ruby的数组支持任意起始下标       assert_equal(nil, a.rindex("z"))
138让Ruby的数组支持任意起始下标     end
139让Ruby的数组支持任意起始下标     
140让Ruby的数组支持任意起始下标     def test_slice()
141让Ruby的数组支持任意起始下标       a = ['a','b','c','d','e']
142让Ruby的数组支持任意起始下标       a.base_index = 1
143让Ruby的数组支持任意起始下标       assert_equal(1, a.base_index)
144让Ruby的数组支持任意起始下标       assert_equal('a', a.slice(1))
145让Ruby的数组支持任意起始下标       assert_equal(['a','b','c'], a.slice(1,3))
146让Ruby的数组支持任意起始下标       assert_equal(['a','b','c','d'], a.slice(1..4))
147让Ruby的数组支持任意起始下标       assert_equal('e', a.slice(0))
148让Ruby的数组支持任意起始下标       assert_equal(nil, a.slice(6))
149让Ruby的数组支持任意起始下标       assert_equal([], a.slice(6,1))
150让Ruby的数组支持任意起始下标       assert_equal([], a.slice(6..10))
151让Ruby的数组支持任意起始下标       assert_equal(nil, a.slice(7,1))
152让Ruby的数组支持任意起始下标     end
153让Ruby的数组支持任意起始下标     
154让Ruby的数组支持任意起始下标     def test_slice!()
155让Ruby的数组支持任意起始下标       a = ['a','b','c']
156让Ruby的数组支持任意起始下标       a.base_index = 1
157让Ruby的数组支持任意起始下标       
158让Ruby的数组支持任意起始下标       assert_equal('b', a.slice!(2))
159让Ruby的数组支持任意起始下标       assert_equal(['a','c'], a)
160让Ruby的数组支持任意起始下标       assert_equal(nil, a.slice!(100))
161让Ruby的数组支持任意起始下标       assert_equal(['a','c'], a)
162让Ruby的数组支持任意起始下标     end
163让Ruby的数组支持任意起始下标     
164让Ruby的数组支持任意起始下标     def test_indexes()
165让Ruby的数组支持任意起始下标       a = [11,22,33,44,55,66,77,88]
166让Ruby的数组支持任意起始下标       a.base_index = 1
167让Ruby的数组支持任意起始下标       
168让Ruby的数组支持任意起始下标       assert_equal([11], a.indexes(1))
169让Ruby的数组支持任意起始下标       assert_equal([11,33], a.indexes(1,3))
170让Ruby的数组支持任意起始下标       assert_equal([11,11], a.indexes(1,1))
171让Ruby的数组支持任意起始下标       assert_equal([11,33,11], a.indexes(1,3,1))
172让Ruby的数组支持任意起始下标       assert_equal([[11,22,33],[22,33,44],77], a.indexes(1..3,2..4,7))
173让Ruby的数组支持任意起始下标       assert_equal([[11,22]], a.indexes(1让Ruby的数组支持任意起始下标3))
174让Ruby的数组支持任意起始下标       assert_equal([[]], a.indexes(2..1))
175让Ruby的数组支持任意起始下标       assert_equal([], a.indexes())
176让Ruby的数组支持任意起始下标       assert_equal([nil,nil], a.indexes(99,97))
177让Ruby的数组支持任意起始下标     end
178让Ruby的数组支持任意起始下标     
179让Ruby的数组支持任意起始下标     def test_indices()
180让Ruby的数组支持任意起始下标       a = [11,22,33,44,55,66,77,88]
181让Ruby的数组支持任意起始下标       a.base_index = 1
182让Ruby的数组支持任意起始下标       
183让Ruby的数组支持任意起始下标       assert_equal([11], a.indices(1))
184让Ruby的数组支持任意起始下标       assert_equal([11,33], a.indices(1,3))
185让Ruby的数组支持任意起始下标       assert_equal([11,11], a.indices(1,1))
186让Ruby的数组支持任意起始下标       assert_equal([11,33,11], a.indices(1,3,1))
187让Ruby的数组支持任意起始下标       assert_equal([[11,22,33],[22,33,44],77], a.indices(1..3,2..4,7))
188让Ruby的数组支持任意起始下标       assert_equal([[11,22]], a.indices(1让Ruby的数组支持任意起始下标3))
189让Ruby的数组支持任意起始下标       assert_equal([[]], a.indices(2..1))
190让Ruby的数组支持任意起始下标       assert_equal([], a.indices())
191让Ruby的数组支持任意起始下标       assert_equal([nil,nil], a.indices(99,97))
192让Ruby的数组支持任意起始下标     end
193让Ruby的数组支持任意起始下标     
194让Ruby的数组支持任意起始下标     def test_values_at()
195让Ruby的数组支持任意起始下标       a = [11,22,33,44,55,66,77,88]
196让Ruby的数组支持任意起始下标       a.base_index = 1
197让Ruby的数组支持任意起始下标       
198让Ruby的数组支持任意起始下标       assert_equal([11], a.values_at(1))
199让Ruby的数组支持任意起始下标       assert_equal([11,33], a.values_at(1,3))
200让Ruby的数组支持任意起始下标       assert_equal([11,11], a.values_at(1,1))
201让Ruby的数组支持任意起始下标       assert_equal([11,33,11], a.values_at(1,3,1))
202让Ruby的数组支持任意起始下标       assert_equal([11,22,33,22,33,44,77], a.values_at(1..3,2..4,7))
203让Ruby的数组支持任意起始下标       assert_equal([11,22], a.values_at(1让Ruby的数组支持任意起始下标3))
204让Ruby的数组支持任意起始下标       assert_equal([], a.values_at(2..1))
205让Ruby的数组支持任意起始下标       assert_equal([], a.values_at())
206让Ruby的数组支持任意起始下标       assert_equal([nil,nil], a.values_at(99,97))
207让Ruby的数组支持任意起始下标     end
208让Ruby的数组支持任意起始下标end

相关文章:

  • 2022-01-07
  • 2022-01-14
  • 2021-08-18
  • 2021-06-13
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-19
相关资源
相似解决方案