Array Flashcards
&
Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates.
usage:
array & other_array
> [1,2] & [2, 3]
=> [2]
*
Repetition—With a String argument, equivalent to self.join(str). Otherwise, returns a new array built by concatenating the int copies of self.
usage:
array * int → an_array
array * str → a_string
> [1,2] * 3
=> [1,2,1,2,1,2]
> [1,2] * ‘,’
=> ‘1,2’
+
Concatenation—Returns a new array built by concatenating the two arrays together to produce a third array.
usage:
array + other_array → an_array
-
Array Difference—Returns a new array that is a copy of the original array, removing any items that also appear in other_array.
usage:
array - other_array → an_array
What does Array#«_space;do and what does it return?
What does it matter what it returns
Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.
usage:
array «_space;obj → array
< = >
Comparison—Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than other_array. Each object in each array is compared (using ). If any value isn‘t equal, then that inequality is the return value. If all the values found are equal, then the return is based on a comparison of the array lengths. Thus, two arrays are ``equal’’ according to Array# if and only if they have the same length and the value of each element is equal to the value of the corresponding element in the other array.
usage:
array < = > other_array → -1, 0, +1
==
Equality—Two arrays are equal if they contain the same number of elements and if each element is equal to (according to Object.==) the corresponding element in the other array.
usage:
array == other_array → bool
array[] or slice()
Element Reference—Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if the index (or starting index) are out of range.
usage: array[index] → obj or nil array[start, length] → an_array or nil array[range] → an_array or nil array.slice(index) → obj or nil array.slice(start, length) → an_array or nil array.slice(range) → an_array or nil
array[]=
ary[index] = obj → obj
ary[start, length] = obj or other_ary or nil → obj or other_ary or nil
ary[range] = obj or other_ary or nil → obj or other_ary or nil
Element Assignment — Sets the element at index, or replaces a subarray from the start index for length elements, or replaces a subarray specified by the range of indices.
If indices are greater than the current capacity of the array, the array grows automatically. Elements are inserted into the array at start if length is zero.
Negative indices will count backward from the end of the array. For start and range cases the starting index is just before an element.
An IndexError is raised if a negative index points past the beginning of the array.
See also #push, and #unshift.
a = Array.new
a[4] = “4”; #=> [nil, nil, nil, nil, “4”]
a[0, 3] = [ ‘a’, ‘b’, ‘c’ ] #=> [“a”, “b”, “c”, nil, “4”]
a[1..2] = [ 1, 2 ] #=> [“a”, 1, 2, nil, “4”]
a[0, 2] = “?” #=> [”?”, 2, nil, “4”]
a[0..2] = “A” #=> [“A”, “4”]
a[-1] = “Z” #=> [“A”, “Z”]
a[1..-1] = nil #=> [“A”, nil]
a[1..-1] = [] #=> [“A”]
a[0, 0] = [ 1, 2 ] #=> [1, 2, “A”]
a[3, 0] = “B” #=> [1, 2, “A”, “B”]
assoc()
Searches through an array whose elements are also arrays comparing obj with the first element of each contained array using obj.==. Returns the first contained array that matches (that is, the first associated array), or nil if no match is found. See also Array#rassoc.
usage:
array.assoc(obj) → an_array or nil
> [ [1, 2], [3,4], [5,6] ].assoc(5)
=> [5, 6]
at()
Returns the element at index. A negative index counts from the end of self. Returns nil if the index is out of range.
usage:
array.at(index) → obj or nil
See also Array#[]. (Array#at is slightly faster than Array#[], as it does not accept ranges and so on.)
clear
Removes all elements from self.
usage:
array.clear → array
collect or map
Invokes block once for each element of self. Creates a new array containing the values returned by the block.
usage:
array. collect {|item| block } → an_array
array. map {|item| block } → an_array
See also Enumerable#collect.
compact
Returns a copy of self with all nil elements removed.
usage:
array.compact → an_array
compact!
Removes nil elements from array. Returns nil if no changes were made.
usage:
array.compact! → array or nil
concat()
Appends the elements in other_array to self.
usage:
array.concat(other_array) → array
What does delete() do and what does it return?
Deletes items from self that are equal to obj. If the item is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found.
usage:
array. delete(obj) → obj or nil
array. delete(obj) { block } → obj or nil
What does delete_at() do and what does it return?
Deletes the element at the specified index, returning that element, or nil if the index is out of range.
usage:
array.delete_at(index) → obj or nil
See also Array#slice!.
delete_if{}
Deletes every element of self for which block evaluates to true.
usage:
array.delete_if {|item| block } → array
What does Array#each{} do and what does it return?
Calls block once for each element in self, passing that element as a parameter.
usage:
array.each {|item| block } → array
What does Array#each_index{} do and what does it return?
Same as Array#each, but passes the index of the element instead of the element itself.
usage:
array.each_index {|index| block } → array
What does Array#empty? do and what does it return?
Returns true if self array contains no elements.
usage:
array.empty? → true or false
eql?
Returns true if array and other_array are the same object, or are both arrays with the same length and have the same content in the same order.
usage:
array.eql?(other_array) → true or false
fetch()
fetch(index) → obj
fetch(index, default) → obj
fetch(index) { |index| block } → obj
Tries to return the element at position index, but throws an IndexError exception if the referenced index lies outside of the array bounds. This error can be prevented by supplying a second argument, which will act as a default value.
Alternatively, if a block is given it will only be executed when an invalid index is referenced. Negative values of index count from the end of the array.
a = [ 11, 22, 33, 44 ]
a.fetch(1) #=> 22
a.fetch(-1) #=> 44
a.fetch(4, ‘cat’) #=> “cat”
a.fetch(100) { |i| puts “#{i} is out of bounds” }
#=> “100 is out of bounds”
fill()
fill(obj) → ary fill(obj, start [, length]) → ary fill(obj, range ) → ary fill { |index| block } → ary fill(start [, length] ) { |index| block } → ary fill(range) { |index| block } → ary
The first three forms set the selected elements of self (which may be the entire array) to obj.
A start of nil is equivalent to zero.
A length of nil is equivalent to the length of the array.
The last three forms fill the array with the value of the given block, which is passed the absolute index of each element to be filled.
Negative values of start count from the end of the array, where -1 is the last element.
a = [ “a”, “b”, “c”, “d” ]
a. fill(“x”) #=> [“x”, “x”, “x”, “x”]
a. fill(“z”, 2, 2) #=> [“x”, “x”, “z”, “z”]
a. fill(“y”, 0..1) #=> [“y”, “y”, “z”, “z”]
a. fill { |i| ii } #=> [0, 1, 4, 9]
a. fill(-2) { |i| ii*i } #=> [0, 1, 8, 27]
first
Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array.
usage:
array. first → obj or nil
array. first(n) → an_array
What does flatten do and what does it return?
Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array.
usage:
array.flatten → an_array
What does flatten! do and what does it return?
Flattens self in place. Returns nil if no modifications were made (i.e., array contains no subarrays.)
usage:
array.flatten! → array or nil
frozen?
Return true if this array is frozen (or temporarily frozen while being sorted).
usage:
array.frozen? → true or false