Array Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

&

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

*

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

+

A

Concatenation—Returns a new array built by concatenating the two arrays together to produce a third array.

usage:
array + other_array → an_array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

-

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does Array#&laquo_space;do and what does it return?

What does it matter what it returns

A

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 &laquo_space;obj → array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

< = >

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

==

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

array[] or slice()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

array[]=

A

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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

assoc()

A

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]­ ].ass­oc(5)
=> [5, 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

at()

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

clear

A

Removes all elements from self.

usage:
array.clear → array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

collect or map

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

compact

A

Returns a copy of self with all nil elements removed.

usage:
array.compact → an_array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

compact!

A

Removes nil elements from array. Returns nil if no changes were made.

usage:
array.compact! → array or nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

concat()

A

Appends the elements in other_array to self.

usage:
array.concat(other_array) → array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does delete() do and what does it return?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does delete_at() do and what does it return?

A

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!.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

delete_if{}

A

Deletes every element of self for which block evaluates to true.

usage:
array.delete_if {|item| block } → array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does Array#each{} do and what does it return?

A

Calls block once for each element in self, passing that element as a parameter.

usage:
array.each {|item| block } → array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does Array#each_index{} do and what does it return?

A

Same as Array#each, but passes the index of the element instead of the element itself.

usage:
array.each_index {|index| block } → array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does Array#empty? do and what does it return?

A

Returns true if self array contains no elements.

usage:
array.empty? → true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

eql?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

fetch()

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

fill()

A
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| i
i*i } #=> [0, 1, 8, 27]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

first

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What does flatten do and what does it return?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What does flatten! do and what does it return?

A

Flattens self in place. Returns nil if no modifications were made (i.e., array contains no subarrays.)

usage:
array.flatten! → array or nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

frozen?

A

Return true if this array is frozen (or temporarily frozen while being sorted).

usage:
array.frozen? → true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

hash

A

Compute a hash-code for this array. Two arrays with the same content will have the same hash code (and will compare using eql?).

usage:
array.hash → fixnum

31
Q

include?

A

Returns true if the given object is present in self (that is, if any object == anObject), false otherwise.

usage:
array.include?(obj) → true or false

32
Q

index

A

Returns the index of the first object in self such that is == to obj. Returns nil if no match is found.

usage:
array.index(obj) → int or nil

33
Q

replace

A

Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.

usage:
array.replace(other_array) → array

34
Q

insert

A

Inserts the given values before the element with the given index (which may be negative).

usage:
array.insert(index, obj…) → array

35
Q

inspect

usage:
array.inspect → string

A

Create a printable version of array.

36
Q

join

A

Returns a string created by converting each element of the array to a string, separated by sep.

usage:
array.join(sep=$,) → str

37
Q

last

A

Returns the last element(s) of self. If the array is empty, the first form returns nil.

usage:

array. last → obj or nil
array. last(n) → an_array

38
Q

length

A

Returns the number of elements in self. May be zero.

usage:
array.length → int

39
Q

collect or map

A

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.

40
Q

collect! or map!

A

Invokes the block once for each element of self, replacing the element with the value returned by block.

usage:
array.collect! {|item| block } → array
array.map! {|item| block } → array
See also Enumerable#collect.

41
Q

nitems

A

Returns the number of non-nil elements in self. May be zero.

usage:
array.nitems → int

42
Q

pack

A

Packs the contents of arr into a binary sequence according to the directives in aTemplateString (see the table below) Directives A,’’a,’’ and Z’’ may be followed by a count, which gives the width of the resulting field. The remaining directives also may take a count, indicating the number of array elements to convert. If the count is an asterisk (*’’), all remaining array elements will be converted. Any of the directives sSiIlL’’ may be followed by an underscore (_’’) to use the underlying platform‘s native size for the specified type; otherwise, they use a platform-independent size. Spaces are ignored in the template string.

usage:
arr.pack ( aTemplateString ) → aBinaryString
See also String#unpack.

43
Q

pop

A

Removes the last element from self and returns it, or nil if the array is empty.

usage:
array.pop → obj or nil

44
Q

push

A

Append—Pushes the given object(s) on to the end of this array. This expression returns the array itself, so several appends may be chained together.

usage:
array.push(obj, … ) → array

45
Q

rassoc

A

Searches through the array whose elements are also arrays. Compares key with the second element of each contained array using ==. Returns the first contained array that matches.

usage:
array.rassoc(key) → an_array or nil
See also Array#assoc.

46
Q

reject

A

Returns a new array containing the items in self for which the block is not true.

usage:
array.reject {|item| block } → an_array

47
Q

reject!

A

Equivalent to Array#delete_if, deleting elements from self for which the block evaluates to true, but returns nil if no changes were made. Also see Enumerable#reject.

usage:
array.reject! {|item| block } → array or nil

48
Q

replace

A

Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.

usage:
array.replace(other_array) → array

49
Q

reverse

A

Returns a new array containing self‘s elements in reverse order.

usage:
array.reverse → new_array

50
Q

reverse!

A

Reverses self in place.

usage:
array.reverse! → array

51
Q

reverse_each

A

Same as Array#each, but traverses self in reverse order.

usage:
array.reverse_each {|item| block }

52
Q

rindex

A

Returns the index of the last object in array == to obj. Returns nil if no match is found.

usage:
array.rindex(obj) → int or nil

53
Q

select

A

Invokes the block passing in successive elements from array, returning an array containing those elements for which the block returns a true value

usage:
array.select {|item| block } → an_array
(equivalent to Enumerable#select).

54
Q

shift

A

Returns the first element of self and removes it (shifting all other elements down by one). Returns nil if the array is empty.

usage:
array.shift → obj or nil

55
Q

size()

A

Alias for length

56
Q

[] or slice

A

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
57
Q

slice!

A

Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subarray, or nil if the index is out of range.

usage:

array. slice!(index) → obj or nil
array. slice!(start, length) → sub_array or nil
array. slice!(range) → sub_array or nil

58
Q

sort

A

Returns a new array created by sorting self. Comparisons for the sort will be done using the operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1.

usage:
array.sort → an_array
array.sort {| a,b | block } → an_array
See also Enumerable#sort_by.

59
Q

sort!

A

Sorts self. Comparisons for the sort will be done using the operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1

usage:

array. sort! → array
array. sort! {| a,b | block } → array

60
Q

to_a

A

Returns self. If called on a subclass of Array, converts the receiver to an Array object.

usage:
array.to_a → array

61
Q

to_ary

A

Returns self.

usage:
array.to_ary → array

62
Q

to_s

A

Returns self.join.

usage:
array.to_s → string

63
Q

transpose

A

Assumes that self is an array of arrays and transposes the rows and columns.

usage:
array.transpose → an_array

64
Q

uniq

A

Returns a new array by removing duplicate values in self.

usage:
array.uniq → an_array

65
Q

uniq!

A

Removes duplicate elements from self. Returns nil if no changes are made (that is, no duplicates are found).

usage:
array.uniq! → array or nil

66
Q

unshift

A

Prepends objects to the front of array. other elements up one.

usage:
array.unshift(obj, …) → array

67
Q

values_at

A

Returns an array containing the elements in self corresponding to the given selector(s). The selectors may be either integer indices or ranges.

usage:
array.values_at(selector,… ) → an_array

68
Q

zip

A

Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument. This generates a sequence of self.size n-element arrays, where n is one more that the count of arguments. If the size of any argument is less than enumObj.size, nil values are supplied. If a block given, it is invoked for each output array, otherwise an array of arrays is returned.

usage:

array. zip(arg, …) → an_array
array. zip(arg, …) {| arr | block } → nil

69
Q

|

A

Set Union—Returns a new array by joining this array with other_array, removing duplicates.

usage:
array | other_array → an_array

70
Q

abbrev

A

abbrev(pattern = nil)
Calculates the set of unambiguous abbreviations for the strings in self.

require 'abbrev'
%w{ car cone }.abbrev
#=> {"ca" => "car", "con"=>"cone", "co" => "cone",
     "car"=>"car", "cone" => "cone"}

The optional pattern parameter is a pattern or a string. Only input strings that match the pattern or start with the string are included in the output hash.

%w{ fast boat day }.abbrev(/^.a/)
#=> {"fas"=>"fast", "fa"=>"fast", "da"=>"day",
     "fast"=>"fast", "day"=>"day"}
See also Abbrev.abbrev
71
Q

How would you remove 3 from this_array?

this_array = [1,2,3,4,5,6,7]

A

this_array.delete(3)

72
Q

What is the difference between Array#reject and Array#delete_if ?

A

Array#reject will return a new array.

Array#delete_if will mutate the object and return itself.

73
Q

What would a equal after the follow executes?

a = [1,2,3,4,5]
a[1,2] = [1,2,3,4,5]
A

[1,1,2,3,4,5,4,5]

74
Q

What do a, b, c and d equal?

a = [1,2,3].first
b = [1,2,3].first(2)
c = [].first
d = [].first(2)
A

a: 1
b: [1,2]
c: nil
d: [ ]