Array Flashcards

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

Ruby Array

return the first n elements of an array

A

take(n) => new_ary

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

Ruby Array

return a new array containing elements common to the two arrays

A

ary & other_ary => new_ary

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

Ruby Array

return a new array built by concatenating the two arrays together to produce a third array

A

concat(other_ary) => ary

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

Ruby Array

return a new array that is a copy of the original array, removing any items that also appear in other_ayr

A

ary - other_ary => new_ary

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

Ruby Array

append - pushes the given object to the end of this array

A

ary << obj => ary

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

Ruby Array

search through an array whose elements are also arrays comparing obj with the first element of each contained array; return the first contained array that matches, or nil if no match is found

A

assoc(obj) => element_ary or nil

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

Ruby Array

return the element at index; a negative index counts from the end of self; return nil if the index is out of range

A

at(index) => obj or nil

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

Ruby Array

remove all elements of self

A

clear => ary

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

Ruby Array

yield all combinations of length n of elements from the array

A

combination(n) => Enumerator

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

Ruby Array

return a copy of self with all nil elements removed

A

compact => new_ary

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

Ruby Array

alias for Array#concat

A

ary + other_ary => new_ary

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

Ruby Array

return the number of elements; if an argument is given, return the number of elements that == obj; if a block is given, return the number of elements for which the block returns true

A

count => int

count(obj) => int

count { |item| block } => int

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

Ruby Array

call the given block n times for each element or forever if nil is given

A

cycle(n=nil) { |obj| block }

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

Ruby Array

delete all items from self that == obj

A

delete(obj) => item or nil

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

Ruby Array

delete the element at the specified index, returning that element or nil if the index is out of range

A

delete_at(index) => obj or nil

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

Ruby Array

return a new array containing elements in self for which the given block is not true

A

reject { |item| block } => new_ary

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

Ruby Array

drop first n elements from ary and returns the remaining elements in an array

A

drop(n) => new_ary

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

Ruby Array

drop elements up to, but not including, the first element for which the block returns nil or false and return an array containing the remaining elements

A

drop_while { |obj| block } => new_ary

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

Ruby Array

call the given block once for each element in self, passing that element as a parameter; return the array itself

A

each { |item| block } => ary

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

Ruby Array

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

A

each_index { |index| block } => ary

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

Ruby Array

return true if self contains no elements

A

empty? => true or false

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

Ruby Array

set the selected elements of self to obj; if a block is given, fills the array with the value of the block, which is passed the index of each element to be filled

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

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

Ruby Array

return the index of the first element that == obj; if a block is given, return the index of the first element for which the block returns true

A

find_index(obj) => int or nil

find_index { |item| block } => int or nil

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

Ruby Array

return the first n elements of self

A

first => obj or nil

first(n) => new_ary

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

Ruby Array

return a new array that is a one-dimensional flattening of self; optional level argument determines the level of recursion to flatten

A

flatten => new_ary

flatten(level) => new_ary

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

Ruby Array

return true if obj is present in self (any element == obj)

A

include?(obj) => true or false

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

Ruby Array

return the index of the first element of self that == obj; if a block is given, return the first element for which the block returns true

A

index(obj) => int or nil

index { |item| block } => int or nil

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

Ruby Array

insert the given values before the element at the specified index; if a negative index is given, the elements are inserted after the element that index (i.e. -1 adds to the end of the array)

A

insert(index, obj…) => ary

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

Ruby Array

return a string created by converting each element of self to a string, separated by the given separator or an empty string

A

join(separator=$,) => str

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

Ruby Array

return the last n elements of self

A

last => obj or nil

last(n) => new_ary

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

Ruby Array

return the number of elements in self

A

length => int

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

Ruby Array

return a new array containing the values returned by the block, which is invoked once for each element of self

A

map { |item| block } => new_ary

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

Ruby Array

yield all permutations of length n of the elements of self, then return the array itself

A

permutation(n) => Enumerator

permutation(n) { |p| block } => ary

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

Ruby Array

remove the last n elements of self and return them

A

pop => obj or nil

pop(n) => new_ary

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

Ruby Array

return an array of all combinations of elements of all arrays

A

product(other_ary, …) => new_ary

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

Ruby Array

append - push the given object(s) on to the end of self, returning the array itself

A

push(obj, …) => ary

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

Ruby Array

search through self whose elements are also arrays; compare obj with the second element of each contained array; return the first contained array that matches obj

A

rassoc(obj) => element or nil

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

Ruby Array

yield all repeated combinations of length n of elements of self then returns the array itself

A

repeated_combination(n) => Enumerator

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

Ruby Array

yield all repeated permuations of length n of the elements of self, then return the array itself

A

repeated_permutation(n) => Enumerator

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

Ruby Array

return a new array containing self’s elements in reverse order

A

reverse => new_ary

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

Ruby Array

same as Array#each but traverses self in reverse order

A

reverse_each { |item| block } => ary

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

Ruby Array

return the index of the last element in self that == obj; if a block is given, return the index of the last element in self for which the block returns true

A

rindex(obj) => int or nil

rindex(obj) { |item| block } => int or nil

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

Ruby Array

return a new array by rotating self so that the element at count is the first element of the new array

A

rotate(count=1) => new_ary

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

Ruby Array

choose a random element or n random elements from self

A

sample => obj

sample(n) => new_ary

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

Ruby Array

return a new array containing all elements of self for which the block returns a true value

A

select { |item| block } => new_ary

46
Q

Ruby Array

remove the first element of self and returns it (shifting all other elements down by one); returns nil if empty

A

shift => obj

47
Q

Ruby Array

return a new array with the elements of self shuffled

A

shuffle => new_ary

48
Q

Ruby Array

return a new array created by sorting self; comparisons done using ; if a block is given, it must implement a comparison between a and b and return a negative integer if a

A

sort => new_ary

sort { |a, b| block } => new_ary

49
Q

Ruby Array

return the first n elements of self

A

take(n) => new_ary

50
Q

Ruby Array

pass elements of self to the block until the block returns nil or false; returns an array of all prior elements

A

take_while { |obj| block } => new_ary

51
Q

Ruby Array

assume that self is an array of arrays and transposes rows and columns

A

transpose => new_ary

52
Q

Ruby Array

return a new array by removing duplicate values in self; if a block is given, use the return value of the block for comparison

A

uniq => new_ary

uniq { |item| block } => new_ary

53
Q

Ruby Array

prepend obj to the front of self, moving other elements upwards

A

unshift(obj, …) => ary

54
Q

Ruby Array

convert any arguments to arrays, then merges elements of self with corresponding elements from each argument; if a block is given, it’s invoked for each output array, otherwise an array of arrays is returned

A

zip(arg, …) => new_ary

zip(arg, …) { |arr| block } => nil

55
Q

Ruby Array

set union - return a new array by joining ary and other_ary, excluding any duplicates and preserving the order from the original array

A

ary | other_ary => new_ary

56
Q

Ruby Array

take(n) => new_ary

A

return the first n elements of an array

57
Q

Ruby Array

ary & other_ary => new_ary

A

return a new array containing elements common to the two arrays

58
Q

Ruby Array

concat(other_ary) => ary

A

return a new array built by concatenating the two arrays together to produce a third array

59
Q

Ruby Array

ary - other_ary => new_ary

A

return a new array that is a copy of the original array, removing any items that also appear in other_ayr

60
Q

Ruby Array

ary << obj => ary

A

append - pushes the given object to the end of this array

61
Q

Ruby Array

assoc(obj) => element_ary or nil

A

search through an array whose elements are also arrays comparing obj with the first element of each contained array; return the first contained array that matches, or nil if no match is found

62
Q

Ruby Array

at(index) => obj or nil

A

return the element at index; a negative index counts from the end of self; return nil if the index is out of range

63
Q

Ruby Array

clear => ary

A

remove all elements of self

64
Q

Ruby Array

combination(n) => Enumerator

A

yield all combinations of length n of elements from the array

65
Q

Ruby Array

compact => new_ary

A

return a copy of self with all nil elements removed

66
Q

Ruby Array

ary + other_ary => new_ary

A

alias for Array#concat

67
Q

Ruby Array

count => int

count(obj) => int

count { |item| block } => int

A

return the number of elements; if an argument is given, return the number of elements that == obj; if a block is given, return the number of elements for which the block returns true

68
Q

Ruby Array

cycle(n=nil) { |obj| block }

A

call the given block n times for each element or forever if nil is given

69
Q

Ruby Array

delete(obj) => item or nil

A

delete all items from self that == obj

70
Q

Ruby Array

delete_at(index) => obj or nil

A

delete the element at the specified index, returning that element or nil if the index is out of range

71
Q

Ruby Array

reject { |item| block } => new_ary

A

return a new array containing elements in self for which the given block is not true

72
Q

Ruby Array

drop(n) => new_ary

A

drop first n elements from ary and returns the remaining elements in an array

73
Q

Ruby Array

drop_while { |obj| block } => new_ary

A

drop elements up to, but not including, the first element for which the block returns nil or false and return an array containing the remaining elements

74
Q

Ruby Array

each { |item| block } => ary

A

call the given block once for each element in self, passing that element as a parameter; return the array itself

75
Q

Ruby Array

each_index { |index| block } => ary

A

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

76
Q

Ruby Array

empty? => true or false

A

return true if self contains no elements

77
Q

Ruby Array

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

A

set the selected elements of self to obj; if a block is given, fills the array with the value of the block, which is passed the index of each element to be filled

78
Q

Ruby Array

find_index(obj) => int or nil

find_index { |item| block } => int or nil

A

return the index of the first element that == obj; if a block is given, return the index of the first element for which the block returns true

79
Q

Ruby Array

first => obj or nil

first(n) => new_ary

A

return the first n elements of self

80
Q

Ruby Array

flatten => new_ary

flatten(level) => new_ary

A

return a new array that is a one-dimensional flattening of self; optional level argument determines the level of recursion to flatten

81
Q

Ruby Array

include?(obj) => true or false

A

return true if obj is present in self (any element == obj)

82
Q

Ruby Array

index(obj) => int or nil

index { |item| block } => int or nil

A

return the index of the first element of self that == obj; if a block is given, return the first element for which the block returns true

83
Q

Ruby Array

insert(index, obj…) => ary

A

insert the given values before the element at the specified index; if a negative index is given, the elements are inserted after the element that index (i.e. -1 adds to the end of the array)

84
Q

Ruby Array

join(separator=$,) => str

A

return a string created by converting each element of self to a string, separated by the given separator or an empty string

85
Q

Ruby Array

last => obj or nil

last(n) => new_ary

A

return the last n elements of self

86
Q

Ruby Array

length => int

A

return the number of elements in self

87
Q

Ruby Array

map { |item| block } => new_ary

A

return a new array containing the values returned by the block, which is invoked once for each element of self

88
Q

Ruby Array

permutation(n) => Enumerator

permutation(n) { |p| block } => ary

A

yield all permutations of length n of the elements of self, then return the array itself

89
Q

Ruby Array

pop => obj or nil

pop(n) => new_ary

A

remove the last n elements of self and return them

90
Q

Ruby Array

product(other_ary, …) => new_ary

A

return an array of all combinations of elements of all arrays

91
Q

Ruby Array

push(obj, …) => ary

A

append - push the given object(s) on to the end of self, returning the array itself

92
Q

Ruby Array

rassoc(obj) => element or nil

A

search through self whose elements are also arrays; compare obj with the second element of each contained array; return the first contained array that matches obj

93
Q

Ruby Array

repeated_combinations(n) => Enumerator

A

yield all repeated combinations of length n of elements of self then returns the array itself

94
Q

Ruby Array

repeated_permutation(n) => Enumerator

A

yield all repeated permuations of length n of the elements of self, then return the array itself

95
Q

Ruby Array

reverse => new_ary

A

return a new array containing self’s elements in reverse order

96
Q

Ruby Array

reverse_each { |item| block } => ary

A

same as Array#each but traverses self in reverse order

97
Q

Ruby Array

rindex(obj) => int or nil

rindex(obj) { |item| block } => int or nil

A

return the index of the last element in self that == obj; if a block is given, return the index of the last element in self for which the block returns true

98
Q

Ruby Array

rotate(count=1) => new_ary

A

return a new array by rotating self so that the element at count is the first element of the new array

99
Q

Ruby Array

sample => obj

sample(n) => new_ary

A

choose a random element or n random elements from self

100
Q

Ruby Array

select { |item| block } => new_ary

A

return a new array containing all elements of self for which the block returns a true value

101
Q

Ruby Array

shift => obj

A

remove the first element of self and returns it (shifting all other elements down by one); returns nil if empty

102
Q

Ruby Array

shuffle => new_ary

A

return a new array with the elements of self shuffled

103
Q

Ruby Array

sort => new_ary

sort { |a, b| block } => new_ary

A

return a new array created by sorting self; comparisons done using ; if a block is given, it must implement a comparison between a and b and return a negative integer if a

104
Q

Ruby Array

take(n) => new_ary

A

return the first n elements of self

105
Q

Ruby Array

take_while { |obj| block } => new_ary

A

pass elements of self to the block until the block returns nil or false; returns an array of all prior elements

106
Q

Ruby Array

transpose => new_ary

A

assume that self is an array of arrays and transposes rows and columns

107
Q

Ruby Array

uniq => new_ary

uniq { |item| block } => new_ary

A

return a new array by removing duplicate values in self; if a block is given, use the return value of the block for comparison

108
Q

Ruby Array

unshift(obj, …) => ary

A

prepend obj to the front of self, moving other elements upwards

109
Q

Ruby Array

zip(arg, …) => new_ary

zip(arg, …) { |arr| block } => nil

A

convert any arguments to arrays, then merges elements of self with corresponding elements from each argument; if a block is given, it’s invoked for each output array, otherwise an array of arrays is returned

110
Q

Ruby Array

ary | other_ary => new_ary

A

set union - return a new array by joining ary and other_ary, excluding any duplicates and preserving the order from the original array