Class: Array

Inherits:
Object show all
Defined in:
lib/ruby/array.rb

Selection (collapse)

Filtering (collapse)

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) defined_at?(n)

True if #at is defined for the given n

Examples:

[1, 2, 3].defined_at?(0)    #=> true
[].defined_at?(0)           #=> false

Returns:

  • (Boolean)


20
21
22
# File 'lib/ruby/array.rb', line 20

def defined_at?(n)
  n < length and -n <= length
end

- (Array) drop(n)

Select all elements except the first n ones.

Examples:

[1, 2, 3].drop(1)   #=> [2, 3]
[1, 3, 3].drop(2)   #=> [3]
[].drop(10)         #=> []

Returns:

Raises:

  • (ArgumentError)


61
62
63
64
# File 'lib/ruby/array.rb', line 61

def drop(n)
  raise ArgumentError, "n cannot be negative" if n < 0
  slice(n..-1) or []
end

- (Array) drop_until(&block)

Drops the longest prefix of elements that do not satisfy the predicate.

Returns:



111
112
113
114
115
116
117
118
# File 'lib/ruby/array.rb', line 111

def drop_until(&block)
  # This is in tail call form
  unless empty? or yield(head)
    tail.drop_until(&block)
  else
    self
  end
end

- (Array) drop_while(&block)

Drops the longest prefix of elements that satisfy the predicate.

Returns:



99
100
101
102
103
104
105
106
# File 'lib/ruby/array.rb', line 99

def drop_while(&block)
  # This is in tail call form
  if not empty? and yield(head)
    tail.drop_while(&block)
  else
    self
  end
end

- head

Return the first item. Raises an IndexError if the Array is empty?.

Examples:

[1, 2, 3].head  #=> 1

Raises:

  • (IndexError)


8
9
10
11
12
# File 'lib/ruby/array.rb', line 8

def head
  raise IndexError, "head of empty list" if empty?
  x, = self
  x
end

- (Array) init(n = 1)

Selects all elements except the last n ones.

Examples:

[1, 2, 3].init      #=> [1, 2]
[1, 2, 3].init(2)   #=> [1]
[].tail             #=> []

Returns:

Raises:

  • (ArgumentError)


48
49
50
51
# File 'lib/ruby/array.rb', line 48

def init(n = 1)
  raise ArgumentError, "n cannot be negative" if n < 0
  slice(0..-(n + 1)) or []
end

- ([Array]) runs(&block)

Returns a list of sublists, where each sublist contains only equal elements. Equality is determined by a two-argument block parameter. The concatenation of the result is equal to the original argument.

Examples:

"abba".split(//).group_seq(&:==) #=> [["y"], ["a"], ["b", "b"], ["a"]]

Returns:



170
171
172
173
174
175
176
177
# File 'lib/ruby/array.rb', line 170

def runs(&block)
  unless empty?
    as, bs = tail.split_until{|x| block.call(head, x) }
    head.cons(as).cons(bs.runs(&block))
  else
    []
  end
end

- ((Array, Array)) split_at(n)

Split the array in two at the given position.

Examples:

[1, 2, 3].split_at(2)   #=> [[1,2], [3]]
[1, 2, 3].split_at(0)   #=> [[], [1,2,3]]

Returns:



85
86
87
88
# File 'lib/ruby/array.rb', line 85

def split_at(n)
  n = length + n if n < 0
  return take(n), drop(n)
end

- ((Array, Array)) split_until(&block)

Splits the array into prefix/suffix pair according to the predicate.

Returns:



147
148
149
150
151
# File 'lib/ruby/array.rb', line 147

def split_until(&block)
  prefix = take_while(&block)
  suffix = drop(prefix.length)
  return prefix, suffix
end

- ((Array, Array)) split_when(&block)

Splits the array into prefix/suffix pair according to the predicate.

Returns:



156
157
158
159
160
# File 'lib/ruby/array.rb', line 156

def split_when(&block)
  prefix = take_until(&block)
  suffix = drop(prefix.length)
  return prefix, suffix
end

- (Array) tail

Selects all elements except the first.

Examples:

[1, 2, 3].tail  #=> [2, 3]
[1].tail        #=> []
[].tail         #=> []

Returns:



35
36
37
38
# File 'lib/ruby/array.rb', line 35

def tail
  _, *xs = self
  xs
end

- (Array) take(n)

Select the first n elements.

Examples:

[1, 2, 3].take(2)  #=> [1, 2]
[1, 2, 3].take(0)  #=> []

Returns:

Raises:

  • (ArgumentError)


73
74
75
76
# File 'lib/ruby/array.rb', line 73

def take(n)
  raise ArgumentError, "n cannot be negative" if n < 0
  slice(0, n) or []
end

- (Array) take_until(accumulator = [], &block)

Takes the longest prefix of elements that do not satisfy the predicate.

Returns:



135
136
137
138
139
140
141
142
# File 'lib/ruby/array.rb', line 135

def take_until(accumulator = [], &block)
  # This is in tail call form
  unless empty? or yield(head)
    tail.take_until(head.snoc(accumulator), &block)
  else
    accumulator
  end
end

- (Array) take_while(accumulator = [], &block)

Takes the longest prefix of elements that satisfy the predicate.

Returns:



123
124
125
126
127
128
129
130
# File 'lib/ruby/array.rb', line 123

def take_while(accumulator = [], &block)
  # This is in tail call form
  if not empty? and yield(head)
    tail.take_while(head.snoc(accumulator), &block)
  else
    accumulator
  end
end