Arrays Flashcards

1
Q

How can you create arrays?

A

1) variable = [1, 2, 3]
2) variable = Array.new
then add elements by id number y[0] = 543
3) nums = Array.
4) nums = Array[1, 2, 3, 4,5]
5) array = %w{ a b c d }

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

How do you delete values from arrays?

A

With the delete method

array.delete(value)

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

How do you delete an element from an array using the index?

A

With the delete at method

array.delete_at(index_number)

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

How do you delete elements from array that fit into an criteria?

A

with the delete if method

array.delete_if { |x| some_code }

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

What is the join method?

A

The join method returns a string created by converting each element of the array to a string, separated by the given separator. If the separator is nil, it uses current $,. If both the separator and $, are nil, it uses empty string.

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

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

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

What is the pop method?

A

It allows you to delete the last element of an array

1) array.pop

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

What is the push method?

A

it allows you to add an element to end of an array
1) array.push(element)
2 array.push(element1, element2, element3)

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

What is an hash?

A

The hash is an key:value collection that can store values.

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

How do you create a new hash?

A

1) hash = { first_key: ‘first element’ , second_key: ‘second_element’ } (this is the modern way to make a hash)
2) hash = { ‘first_key’ => ‘first_element’ }
3) hash = { :fist_key => ‘first element’ }

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

How do you grab an element from an hash?

A

hash{:chosen_key]

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

How do you delete elements from a hash?

A

you use the delete method

hash.delete(:key)

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

How do you iterate over just a hash key?

A

With the each_key method

hash.each_key do |key|
some_code
end

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

How do you iterate over just a hash value?

A

With the each_value method

hash.each_value do |value|
some_code
end

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

How do you add to a hash?

A

hash[:key] = value

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

How do you invert the key and the values in a hash?

A

You use the .invert method

hash.invert

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

How do you merge two hashes?

A

With the .merge method

hash1.merge(hash_2)

17
Q

How do we convert a hash into an array?

A

With the to_a method

hash_1.to_a

18
Q

How do you view all keys in a hash?

A

With the .key method

hash_1.key

19
Q

How do you view all values in a hash?

A

with the .value metod

hash_1.value