Instance Methods Flashcards

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

join

A

Fn: Return a string which is the values in the array converted to strings and concatenated.
Returns: string
Syntax: my_array.join(separator)

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

Fn: put the array items in random order
Returns: Array (!) or new array

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

each

A

Function: Iterates through the array, applying the block once to each item
Returns: The original array
Syntax: my_array.each {|item| block}
(Returns Enumerator if no block is given)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
#map
#map!
#collect
#collect!
A

Fn: Transform an array by iterating through and passing each element as a parameter.
Returns: The first method returns a new array.
The second, with the bang suffix, mutates the original array in place and returns it.
Syntax: same as #each
(Returns Enumerator if no block is given)

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

Fn: removes duplicates. Use return value of block for comparison
Returns: array (!) or new array
Syntax: my_array.uniq{|item|}

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

Fn: sort using a set of keys generated by mapping the values in the array through the given block
Returns: a new array (or mutates array in place when !)
Syntax: my_array.sort_by{|object| block}

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

keep_if

A

Fn: evaluates the block and deletes every element from the array for which it evaluates to false
Returns: the original array
Syntax: same as #delete_if

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

each_index

A

Function: Like each, but passes the index of the element instead of the element itself
Returns: the original array
Syntax: my_array.each_index{|item| block}

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

Fn: take is like first(n). Drop returns the elements after the first n (i.e. the remaining ones)
Returns: new array
Syntax: my_array.take(n)

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

delete

A

Fn: destructively remove an element from the array matching obj
Returns: the last deleted item
Syntax: my_array.delete(obj)

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

values_at

A

Fn: return the values at the given indices
Returns: new array
Syntax: my_array.values_at(int1, in2… or a range)

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

+ / #concat

A

Fn: concatenate two arrays
Returns: a new array
Syntax: my_array + other_array

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

Fn: rotates the array elements so that count is the first element
Returns: Array (!) or new array
Syntax: my_array.rotate(count=n)

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

delete_at

A

Fn: destructively remove the object found at a given index
Returns: the deleted object or nil if not found
Syntax: my_array.delete_at(idx)

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

Fn: make a one dimensional array
Returns: array (!) or a new array
Syntax: my_array.flatten([level])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
#difference
[ ]-
A

Makes a new array that is a copy of the receiver, minus the duplicates, preserving order

17
Q
#first
#last
A

Fn: return the first/last (or first/last n) elements from the array
Returns: either the element or an array of n elements
Syntax: my_array.first(n)

18
Q
#slice
#slice!
A

Fn: return a sub-array starting at idx, for a given length/range (optional)
Returns: the array (!) or a new array
Syntax: my_array.slice(idx,length/range) or nil

19
Q

insert

A

Fn: Inserts object after idx
Returns: the array
Syntax: my_array.insert(idx, obj)

20
Q

to_s / #inspect

A

Fn: returns the string interpretation of the array
Returns: a string

21
Q

length / #size

A

Returns number of elements in array

22
Q
#empty?
#include?(obj)
A

These return Boolean values after checking if the array is empty or if it contains (obj)

23
Q
#union
|
A

Joins arrays, excluding duplicates, preserving order

24
Q

transpose

A

Given an array of arrays transposes the rows and columns

25
Q
#shift
#pop
A

Fn: remove the first/last element from the array
Returns: the removed element (or nil)
Syntax: my_array.shift

26
Q
#select
#select!
#filter
#filter!
A

Fn: iterates through the array, passing each element as parameter to the block and returning it if the block evaluates to ‘true’
Returns: a new array containing the selected results (the methods with a bang suffix mutate the array in place)
Syntax: same as #each
(Returns Enumerator if no block is given)

27
Q
#reverse
#reverse!
A

Fn: put the array elements in reverse order
Returns: the array(!) or a new array

28
Q

sample

A

Fn: Choose a random element from the array

See docs for further usage

29
Q

fetch(idx)

A

Fn: This is the most basic usage which returns the element at idx

30
Q
#sort
#sort!
A

Fn: comparisons for the sort are done using the <=> operator or an optional code block… the block implements a comparison of a & b
Returns: a new array
Syntax: my_array.sort{|a, b| block}

31
Q
#unshift
#push / #<
A

Fn: prepend/append the given object onto the array
Returns: the array
Syntax: my_array.unshift(obj)

32
Q

take_while

A

Fn: Passes elements to the block until it returns false/nil
Returns: a new array
Syntax: my_array.take_while{|obj| block }

33
Q

delete_if

A

Fn: Delete from the array if the block evaluates to true
Returns: original array
Syntax: my_array.delete_if{|item| block}