Arrays: Ruby Docs Flashcards

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

What are the two arguments array.new accepts?

A

arr = Array.new(3, “something”)
==> [“something”, “something”, “something”]

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

How else can you create an array (with specific elements)?

A

Array.new([item, item, item])

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

How would you create an array using the index?

A
Array.new(3){ |index| index ** 2 }
------------------
# => [0, 1, 4]    where index is just a variable name for the index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Accessing hash value within an array: Given

[“CAT”, {“HASHTHING”=>”hash value”}] how would you access the hash value

A

a[1][‘HASHTHING] ==> “hash value”

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

How do you return the an array if it is an array or nil if it is not?

A

Array.try_convert( thing to convert)
==> the array if it is one
==.> nil if not

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

Public Instance Method:

Given two arrays, how do you return an array of the intersection (common elements)?

A

The & method

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]

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

Public Instance Method:

Given two arrays, how do you remove any elements of the second array from the first array?

A
Array Difference ( - ) 
[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]  #=>  [ 3, 3, 5 ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Public Instance Method:
How do you compare two arrays?
What will be returned?

A

Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than the other

[ “a”, “a”, “c” ] <=> [ “a”, “b”, “c” ] #=> -1
[ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1

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

When are two arrays equal via == ?

A

When they contain the same number of elements and each one is equal to the corresponding element in the other array

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

what happens when your index range includes an element but the remainder is non existent?

A

returns the element that exists

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