Method: SortedContainers::SortedArray#slice

Defined in:
lib/sorted_containers/sorted_array.rb

#slice(*args) ⇒ Object, Array

Tries to match the behavior of Array#slice

Parameters:

  • args (Integer, Range, Enumerator::ArithmeticSequence)

    The index or range of values to retrieve.

Returns:

  • (Object, Array)

    The value or values at the specified index or range.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sorted_containers/sorted_array.rb', line 149

def slice(*args)
  case args.size
  when 1
    arg = args[0]
    case arg
    when Integer
      get_value_at_index(arg)
    when Range
      get_values_from_range(arg)
    when Enumerator::ArithmeticSequence
      get_values_from_arithmetic_sequence(arg)
    else
      raise TypeError, "no implicit conversion of #{arg.class} into Integer"
    end
  when 2
    start, length = args
    get_values_from_start_and_length(start, length)
  else
    raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1..2)"
  end
end