• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/72

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

72 Cards in this Set

  • Front
  • Back

any?[{}]





returns true or false




any? [{ |obj| block }]

assoc(obj)

Assumes an array is an array of arrays.




Passed an argument.




Returns a new array or nil




Searches the first element of each array.




Returns all the elements of the array if a match is found.




s1 = [ "colors", "red", "blue", "green" ]




s2 = [ "letters", "a", "b", "c" ]




s3 = "foo"




a = [ s1, s2, s3 ]a.assoc("letters") #=> [ "letters", "a", "b", "c" ]a.assoc("foo") #=> nil

at(index)

returns an object or nil




returns the first object at index




a = [ "a", "b", "c", "d", "e" ]




a.at(0) #=> "a"




a.at(-1) #=> "e"

bsearch{}

returns an element




binary search to find a value which meets the given condition

clear

returns an array




removes all elements from self




a = [ "a", "b", "c", "d", "e" ]a.clear #=> [ ]

collect{}




collect!{}

returns a new array




invokes the given block once for each element of self




creates a new array containing the values returned by the block




if no block is given, an Enumerator is returned




a = [ "a", "b", "c", "d" ]




a.collect { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"]




a.map.with_index{ |x, i| x * i } #=> ["", "b", "cc", "ddd"]




a #=> ["a", "b", "c", "d"]




It's the same as map

combination()}

yields all combinations of length n of elements from the array then returns the array itself




a = [1, 2, 3, 4]




a.combination(1).to_a #=> [[1],[2],[3],[4]]




a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]




a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]




a.combination(4).to_a #=> [[1,2,3,4]]




a.combination(0).to_a #=> [[]] # one combination of length 0




a.combination(5).to_a #=> [] # no combinations of length 5

compact




compact!

returns a new array (non-destructive)




returns a copy of self with all the NIL elements removed

concat(other_ary)

appends the contents of other_ary to self




[ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]




a = [ 1, 2, 3 ]




a.concat( [ 4, 5 ] )




a #=> [ 1, 2, 3, 4, 5 ]

count

returns the number of elements




if an argument is given, it counts the number of elements which equal the argument (using ==)




if a block is given, it counts the number of elements for which the block returns a true value




ary = [1, 2, 4, 2]




ary.count #=> 4




ary.count(2) #=> 2




ary.count { |x| x%2 == 0 } #=> 3

cycled

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




returns nil if the loop has finished without getting interrupted




if no block is given, an Enumerator is returned




a = ["a", "b", "c"]




a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever




a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.

delete(obj)




delete(obj) {block}

Deletes all items from self that are equal to obj


returns the last deleted item, or nil if none are found




If optional block is given, the result of the block is returned if the item is not found.




a = [ "a", "b", "b", "b", "c" ]




a.delete("b") #=> "b"a #=> ["a", "c"]




a.delete("z") #=> nil




a.delete("z") { "not found" } #=> "not found"

delete_at(index)



deletes the element at the specified index, returning the element or nil if out of range.




a = ["ant", "bat", "cat", "dog"]




a.delete_at(2) #=> "cat"a #=> ["ant", "bat", "dog"]




a.delete_at(99) #=> nil

delete_if {}

returns an array




deletes every element of self for which the block evaluates to true




The array is changed instantly every time the block is called, not after the iteration is over




see also #reject!




scores = [ 97, 42, 75 ]




scores.delete_if {|score| score < 80 } #=> [97]

drop(n)

returns a new array




drops the first n elements from an array and returns the rest




does not work with negative numbers




see also #take




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




a.drop(3) #=> [4, 5, 0]

drop_while{}

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




see also #take_while




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




a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]

each{}

calls the given block once for each element in self, passing that element as a parameter




a = [ "a", "b", "c" ]




a.each {|x| print x, " -- " }




#> a -- b -- c --

each_index{}

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




a = [ "a", "b", "c" ]




a.each_index {|x| print x, " -- " }




#> 0 -- 1 -- 1 --

empty?

returns true if self contains no elements




[].empty? #=> true

eql?(other)

returns true if self and other are the same object, or both are arrays with the same content

fetch(index)


fetch(index,default


fetch(index){|index|,block}

tries to return the element at position index, but throws an error if the index is outside of the array. This can be prevented by supplying a second argument, default.




If a block is given it will only be executed when an invalid index is referenced. (eg, another way to handle the error)




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(obj)

fills an array with obj.




Lots of different ways to do this.




I think #fill will only change the elements for as many elements there are in the array. Whereas #replace will expand/contract the array based on what you want to "replace" it with.




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| i*i } #=> [0, 1, 4, 9]




aa.fill(-2) { |i| i*i*i } #=> [0, 1, 8, 27]

find_index(obj)


find_obj{}

returns the index of the first object in the array such that the object is == to obj




If a block is given instead of an argument, it returns the index of the first object for which the block returns true.




see also #rindex




a = [ "a", "b", "c" ]




a.index("b") #=> 1




a.index("z") #=> nil




a.index { |x| x == "b" } #=> 1

first


first(n)

returns the first element (or n-elements), of the array.




a = [ "q", "r", "s", "t" ]




a.first #=> "q"




a.first(2) #=> ["q", "r"]

flatten


flatten(level)


flatten!()


flatten!(level)

returns a new array that is a 1-D flattening of self




that is, for every element in the array, it extracts the elements into a new array.




the optional level argument determines the level of recursion to flatten




s = [ 1, 2, 3 ] #=> [1, 2, 3]




t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]




a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]




a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]a = [ 1, 2, [3, [4, 5] ] ]




a.flatten(1) #=> [1, 2, 3, [4, 5]]

frozen?

returns true if the array is frozen

hash

compute a hash-code for this array

include?(object)

returns true if the given object is present in self

index(obj)


index{}

returns the index of the first object in array such that object is == to obj




if a blog is given instead, returns the index of the first object for which the block returns true




see also #rindex




see also #values_at (opposite)




a = [ "a", "b", "c" ]




a.index("b") #=> 1




a.index("z") #=> nil




a.index { |x| x == "b" } #=> 1

initialize_copy(other_arr)

replaces the contents of self with the contents of other_arr, truncating or expanding when necessary

insert(index,obj)

inserts the given values before the element with the given index




negative indices count backwards, where -1 is th elast element. If a neg index is used, the given values will be inserted after that element, so using an index of -1 will insert the values at the end of the array.




a = %w{ a b c d }




a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]




a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]

to_s

creates a string representation of self





[ "a", "b", "c" ].to_s #=> "[\"a\", \"b\", \"c\"]"




note that the above example is making a string of the entire array (quotation marks on the outside of the array brackets)

join(separator=$)

returns a string created by converting each element in the array to a string, separated by the separator. If the separator is nil, it uses current $. If both separator and $ are nil, it uses an empty string




[ "a", "b", "c" ].join #=> "abc"




[ "a", "b", "c" ].join("-") #=> "a-b-c"

keep_if{}

deletes every element of self for which the block evaluates to false.




see also #select!




a = %w{ a b c d e f }




a.keep_if { |v| v =~ /[aeiou]/ } #=> ["a", "e"]





last


last(n(

returns the last element(s) of self.




see also #first




a = [ "w", "x", "y", "z" ]




a.last #=> "z"




lengtha.last(2) #=> ["y", "z"]

length

returns the number of elements in self. may be zero




[ 1, 2, 3, 4, 5 ].length #=> 5




[].length #=> 0

map{}





invokes the given block once for each element of self



creates a new array containing the values returned by the block




see also enumerable#collect (it's the same)




a = [ "a", "b", "c", "d" ]




a.collect { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"]




a.map.with_index{ |x, i| x * i } #=> ["", "b", "cc", "ddd"]




a #=> ["a", "b", "c", "d"]


map!{}

Invokes the given block once for each element of self,replacing the element with the value returned by the block.See also Enumerable#collect.




a = [ "a", "b", "c", "d" ]




a.map! {|x| x + "!" }a #=> [ "a!", "b!", "c!", "d!" ]




a.collect!.with_index {|x, i| x[0...i] }a #=> ["", "b", "c!", "d!"]

pack()

crazy binary stuff!

permutation{}


permutation(){}

when invoked with a block, yield all permutations of length n of the elements of the array, then return the array




if n is not specified, yield all permutations of all elements






a = [1, 2, 3]




a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]




a.permutation(1).to_a #=> [[1],[2],[3]]




a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],


[3,1],[3,2]]a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]




a.permutation(0).to_a #=> [[]] # one permutation of length 0




a.permutation(4).to_a #=> [] # no permutations of length 4

pop()


pop(n)

removes the last element from self and returns it, or nil if empty




if the number n is given, returns an array of the last n elements (or less), just like array.slice!(-n,n) does.




see #push for the opposite effect




a = [ "a", "b", "c", "d" ]




a.pop #=> "d"




a.pop(2) #=> ["b", "c"]a #=> ["a"]

product(other_arry,...)

returns an array of all combinations of elements from all arrays




the length of the returned array is the product of the length of self and the argument arrays




if a block is given, product will yield all combinations and return self instead.




[1,2,3].product([4,5]) #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]




[1,2].product([1,2]) #=> [[1,1],[1,2],[2,1],[2,2]]




[1,2].product([3,4],[5,6]) #=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6], # [2,3,5],[2,3,6],[2,4,5],[2,4,6]]




[1,2].product() #=> [[1],[2]][1,2].product([]) #=> []

push(obj,...)

append. pushes the given object(s) on to the end of this array. This returns the array itself, so severl appends may be chained together.



see #pop for the opposite




a = [ "a", "b", "c" ]




a.push("d", "e", "f") #=> ["a", "b", "c", "d", "e", "f"]




[1, 2, 3,].push(4).push(5) #=> [1, 2, 3, 4, 5]


rassoc(obj)

searches through the array whose elements are also arrays.



compares obj with the second element of each contained array using obj == .




returns the first contained array that matches obj.




see also #assoc




a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]




a.rassoc("two") #=> [2, "two"]




a.rassoc("four") #=> nil


reject{}




reject!{}

returns a new array containing the items in self for which the given block is false



see also #delete_if






repeated_combination(n){}


repeated_combination(n)

when invoked with a block, yields all repeated combinations of length n of elements from the array. Returns the array it self.




a = [1, 2, 3]




a.repeated_combination(1).to_a #=> [[1], [2], [3]]




a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]




a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3], # [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]




a.repeated_combination(4).to_a #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3], # [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3], # [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]




a.repeated_combination(0).to_a #=> [[]] # one combination of length 0



repeated_permutation(n){}


repeated_permutation(n)

When invoked with a block, yield all repeated permutations of lengthn of the elements of the array, then return the array itself.




a = [1, 2]




a.repeated_permutation(1).to_a #=> [[1], [2]]




a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]]




a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2], # [2,1,1],[2,1,2],[2,2,1],[2,2,2]]




a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0

replace(other_ary)

replaces the contents of self with the contents of other_ary, truncating or expanding when necessary.




basically clears the array and adds the new elements.




I think #fill will only change the elements for as many elements there are in the array. Whereas #replace will expand/contract the array based on what you want to "replace" it with.




a = [ "a", "b", "c", "d", "e" ]




a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]




a #=> ["x", "y", "z"]

reverse




reverse!

returns a new array containing self's elements in reverse order




[ "a", "b", "c" ].reverse #=> ["c", "b", "a"]




[ 1 ].reverse #=> [1]

reverse_each{}


same as #each, but traverses self in reverse order. Whereas #reverse reverses the array, #reverse_each simply does #each, but in a reverse order. It's not about flipping the array like #reverse.





a = [ "a", "b", "c" ]




a.reverse_each {|x| print x, " " }




#>c b a

rindex(obj)


rindex{}

returns the index of the last object in self that is == to obj.




if a block is given, it returns the index of the first object for which the block returns true, starting from the last objet.




see also #index




a = [ "a", "b", "b", "b", "c" ]




a.rindex("b") #=> 3




a.rindex("z") #=> nil




a.rindex { |x| x == "b" } #=> 3

rotate(count=1)

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




if count is negative then it rotates in the opposite direction, starting from the end of self where -1 is the last element.




basically shifts them left by default, right with a negative. default is by 1




a = [ "a", "b", "c", "d" ]




a.rotate #=> ["b", "c", "d", "a"]




a #=> ["a", "b", "c", "d"]




a.rotate(2) #=> ["c", "d", "a", "b"]




a.rotate(-3) #=> ["b", "c", "d", "a"]

sample


sample(random:rng)


sample(n)


sample(n,random:rng)

choose a random element or n random elements from the array.




the elements are chosen by using random and unique indices into the array in order to ensure an element doesn't repeat itself unless the array contained duplicates.




the optional rng argument will be used as the random generator




a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]




a.sample #=> 7




a.sample(4) #=> [6, 4, 2, 5]

select{}

returns a new array containing all elements of ary for which the given block returns a true value



see also enumerable#select




[1,2,3,4,5].select { |num| num.even? } #=> [2, 4]




a = %w{ a b c d e f }




a.select { |v| v =~ /[aeiou]/ } #=> ["a", "e"]


select!{}

destructive version of selec{}



see also #keep_if

shift


shift(n)

removes the first element of self and returns it, shifting all elements down by one.



if a number n is given, returns an array of the first n elements (or less), just like array.slice!(0,n).


With ary containing only the remainder elements, not including what was shifted to new_ary.




see #unshift for opposite.




args = [ "-m", "-q", "filename" ]




args.shift #=> "-m"




args #=> ["-q", "filename"]




args = [ "-m", "-q", "filename" ]




args.shift(2) #=> ["-m", "-q"]




args #=> ["filename"]






shuffle


shuffle!


shuffle(random:rng)

returns a new array with elements of self shuffled



the optional rng element will be used as the random number generator




a = [ 1, 2, 3 ] #=> [1, 2, 3]




a.shuffle #=> [2, 3, 1]




a #=> [1, 2, 3]




a.shuffle(random: Random.new(1)) #=> [1, 3, 2]


size()

see #length

slice - element reference




slice(index)


slice(start,length)


slice(range)

returns the element at index, or returns a subarray starting at the start index and continuing for length's elements, or returns a subarray specified by a rand of indices.




Negative indices count back wards from the end (-1).




For start and range cases, the starting index is just before an element. Additionally, an empty array is returned when the starting index for an element range is the end of the array.




a = [ "a", "b", "c", "d", "e" ]




a[2] + a[0] + a[1] #=> "cab"




a[6] #=> nil




a[1, 2] #=> [ "b", "c" ]




a[1..3] #=> [ "b", "c", "d" ]




a[4..7] #=> [ "e" ]




a[6..10] #=> nil




a[-3, 3] #=> [ "c", "d", "e" ]




# special cases




a[5] #=> nil




a[6, 1] #=> nil




a[5, 1] #=> []




a[5..10] #=> []

slice!(index)


slice!(start,length)


slice1(range)

Deletes the element(s) given by an index (optionally up tolength elements) or by a range.




Returns the deleted object (or objects), or nil if theindex is out of range.




a = [ "a", "b", "c" ]




a.slice!(1) #=> "b"




a #=> ["a", "c"]




a.slice!(-1) #=> "c"




a #=> ["a"]




a.slice!(100) #=> nil




a #=> ["a"]

sort


sort{}




sort!


sort!{}

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 must implement a comparison between a andb, and return -1, when a followsb, 0 when a and b areequivalent, or +1 if b follows a.




a = [ "d", "a", "e", "c", "b" ]




a.sort #=> ["a", "b", "c", "d", "e"]




a.sort { |x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]

sort_by!


sort_by!{}

Sorts self in place using a set of keys generated by mappingthe values in self through the given block.

take(n)

returns first n elements from the array



see also #drop




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




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


take_while{}

Passes elements to the block until the block returns nil orfalse, then stops iterating and returns an array of all priorelements.



see also #drop_while




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




a.take_while { |i| i < 3 } #=> [1, 2]

to_a




to_ary





to_a converts the receiver to an array object (only if it's a subclass of array)




to_ary returns self





to_h

Returns the result of interpreting ary as an array of [key,value] pairs.




[[:foo, :bar], [1, 2]].to_h




# => {:foo => :bar, 1 => 2}

to_s

alias for inspect

transpose

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




throws an error if the length of the subarrays don't match




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




a.transpose #=> [[1, 3, 5], [2, 4, 6]]



uniq


uniq{}




uniq!


uniq{}

Returns a new array by removing duplicate values in self.




If a block is given, it will use the return value of the block forcomparison.




It compares values using their hashand eql? methods for efficiency.




a = [ "a", "a", "b", "b", "c" ]




a.uniq # => ["a", "b", "c"]




b = [["student","sam"], ["student","george"], ["teacher","matz"]]




b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]

unshift(obj,...)

Prepends objects to the front of self, moving other elementsupwards.




see #shift for theopposite effect.




a = [ "b", "c", "d" ]




a.unshift("a") #=> ["a", "b", "c", "d"]




a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]

values_at(selector,...)

Returns an array containing the elements in self correspondingto the given selector(s).




The selectors may be either integer indices or ranges.




see also #select.




see also #index (opposite)




a = %w{ a b c d e f }




a.values_at(1, 3, 5) # => ["b", "d", "f"]




a.values_at(1, 3, 5, 7) # => ["b", "d", "f", nil]




a.values_at(-1, -2, -2, -7) # => ["f", "e", "e", nil]




a.values_at(4..6, 3...6) # => ["e", "f", nil, "d", "e", "f"]

zip(arg,...)




zip(arg,..){}

Converts any arguments to arrays, then merges elements of selfwith corresponding elements from each argument.




This generates a sequence of ary.size n-elementarrays, where n is one more than the count of arguments.




If the size of any argument is less than the size of the initial array,nil values are supplied.




If a block is given, it is invoked for each output array,otherwise an array of arrays is returned.




a = [ 4, 5, 6 ]




b = [ 7, 8, 9 ]




[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]




[1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]




a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]