kernel/common/array.rb
# Fill some portion of the Array with a given element. The
# element to be used can be either given as the first argument
# or as a block that takes the index as its argument. The
# section that is to be filled can be defined by the following
# arguments. The first following argument is either a starting
# index or a Range. If the first argument is a starting index,
# the second argument can be the length. No length given
# defaults to rest of Array, no starting defaults to 0. Negative
# indices are treated as counting backwards from the end. Negative
# counts leave the Array unchanged. Returns self.
#
# array.fill(obj)                                -> array
# array.fill(obj, start [, length])              -> array
# array.fill(obj, range)                         -> array
# array.fill {|index| block }                    -> array
# array.fill(start [, length]) {|index| block }  -> array
# array.fill(range) {|index| block }             -> array
#
Fill some portion of the Array with a given element. The
element to be used can be either given as the first argument
or as a block that takes the index as its argument. The
section that is to be filled can be defined by the following
arguments. The first following argument is either a starting
index or a Range. If the first argument is a starting index,
the second argument can be the length. No length given
defaults to rest of Array, no starting defaults to 0. Negative
indices are treated as counting backwards from the end. Negative
counts leave the Array unchanged. Returns self.


Fill some portion of the Array with a given element . The element to be used can be either given as the first argument or as a block that takes the index as its argument . The section that is to be filled can be defined by the following arguments . The first following argument is either a starting index or a Range . If the first argument is a starting index the second argument can be the length . No length given defaults to rest of Array no starting defaults to 0 . Negative indices are treated as counting backwards from the end . Negative counts leave the Array unchanged . Returns self .
def fill(*args)
    raise ArgumentError, "Wrong number of arguments" if block_given? and args.size > 2
    raise ArgumentError, "Wrong number of arguments" if !block_given? and args.size == 0
    raise ArgumentError, "Wrong number of arguments" if args.size > 3
    start, finish, obj = 0, (@total - 1), nil
    obj = args.shift unless block_given?
    one, two = args.at(0), args.at(1)
    if one.kind_of? Range
      raise TypeError, "Length invalid with range" if args.size > 1   
      start  = Type.coerce_to one.begin, Fixnum, :to_int
      finish = Type.coerce_to one.end, Fixnum, :to_int
      start += @total if start < 0
      finish += @total if finish < 0
      if one.exclude_end?
        return self if start == finish
        finish -= 1
      end
      raise RangeError, "#{one.inspect} out of range" if start < 0
      return self if finish < 0           
    else
      if one
        start = Type.coerce_to one, Fixnum, :to_int
        start += @total if start < 0
        start = 0 if start < 0            
        if two
          finish = Type.coerce_to two, Fixnum, :to_int
          return self if finish < 1       
          raise ArgumentError, "argument too big" if finish < 0 && start < finish.abs
          finish = start + finish - 1
        end
      end
    end                                   
    unless finish < @total
      nt = finish + 1
      reallocate(nt) if @tuple.size < nt
      @total = finish + 1
    end
    if block_given?
      start.upto(finish) { |i|  @tuple.put @start + i, yield(i) }
    else
      start.upto(finish) { |i|  @tuple.put @start + i, obj }
    end
    self
  end
def fill ( * args ) raise ArgumentError , "Wrong number of arguments" if block_given? and args . size > 2 raise ArgumentError , "Wrong number of arguments" if ! block_given? and args . size == 0 raise ArgumentError , "Wrong number of arguments" if args . size > 3 start , finish , obj = 0 , ( @total - 1 ) , nil obj = args . shift unless block_given? one , two = args . at ( 0 ) , args . at ( 1 ) if one . kind_of? Range raise TypeError , "Length invalid with range" if args . size > 1 start = Type . coerce_to one . begin , Fixnum , :to_int finish = Type . coerce_to one . end , Fixnum , :to_int start += @total if start < 0 finish += @total if finish < 0 if one . exclude_end? return self if start == finish finish -= 1 end raise RangeError , "#{one.inspect} out of range" if start < 0 return self if finish < 0 else if one start = Type . coerce_to one , Fixnum , :to_int start += @total if start < 0 start = 0 if start < 0 if two finish = Type . coerce_to two , Fixnum , :to_int return self if finish < 1 raise ArgumentError , "argument too big" if finish < 0 && start < finish . abs finish = start + finish - 1 end end end unless finish < @total nt = finish + 1 reallocate ( nt ) if @tuple . size < nt @total = finish + 1 end if block_given? start . upto ( finish ) { | i | @tuple . put @start + i , yield ( i ) } else start . upto ( finish ) { | i | @tuple . put @start + i , obj } end self end
Fill some portion of the Array with a given element.
Fill some portion of the Array with a given element .
# Normalise arguments # WTF, MRI, TypeError? # Nothing to modify # MRI comp adjusts to 0 # Nothing to modify # Argument normalisation # Adjust the size progressively
args
<not_specific>