Class: Array

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

Selection collapse

Filtering collapse

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/ruby/array.rb', line 3

def blank?
  empty?
end

#defined_at?(n) ⇒ Boolean

True if ‘#at` is defined for the given `n`

Examples:

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/ruby/array.rb', line 28

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

#drop(n) ⇒ Array

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)


69
70
71
72
# File 'lib/ruby/array.rb', line 69

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

#drop_until(&block) ⇒ Array

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

Returns:



119
120
121
122
123
124
125
126
# File 'lib/ruby/array.rb', line 119

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

#drop_while(&block) ⇒ Array

Drops the longest prefix of elements that satisfy the predicate.

Returns:



107
108
109
110
111
112
113
114
# File 'lib/ruby/array.rb', line 107

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

#headObject

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

Examples:

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

Raises:

  • (IndexError)


16
17
18
19
20
# File 'lib/ruby/array.rb', line 16

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

#init(n = 1) ⇒ Array

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)


56
57
58
59
# File 'lib/ruby/array.rb', line 56

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

#present?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/ruby/array.rb', line 7

def present?
  not empty?
end

#runs(&block) ⇒ [Array]

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:



178
179
180
181
182
183
184
185
# File 'lib/ruby/array.rb', line 178

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

#split_at(n) ⇒ (Array, Array)

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:



93
94
95
96
# File 'lib/ruby/array.rb', line 93

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

#split_until(&block) ⇒ (Array, Array)

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

Returns:



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

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

#split_when(&block) ⇒ (Array, Array)

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

Returns:



164
165
166
167
168
# File 'lib/ruby/array.rb', line 164

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

#tailArray

Selects all elements except the first.

Examples:

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

Returns:



43
44
45
46
# File 'lib/ruby/array.rb', line 43

def tail
  _, *xs = self
  xs
end

#take(n) ⇒ Array

Select the first ‘n` elements.

Examples:

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

Returns:

Raises:

  • (ArgumentError)


81
82
83
84
# File 'lib/ruby/array.rb', line 81

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

#take_until(accumulator = [], &block) ⇒ Array

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

Returns:



143
144
145
146
147
148
149
150
# File 'lib/ruby/array.rb', line 143

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

#take_while(accumulator = [], &block) ⇒ Array

Takes the longest prefix of elements that satisfy the predicate.

Returns:



131
132
133
134
135
136
137
138
# File 'lib/ruby/array.rb', line 131

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