Methods Flashcards

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

What is the formal way to declare an array?

A

my_array = Array.new

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

What is the literal way to declare an empty array

A

my_array = []

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

What are two ways to add a new item called “new item” to the end of an array called “my_array”

A

my_array.push(“new item”)
and
my_array &laquo_space;“new item”

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

How would you add an item to the beginning of an array

A

my_array.unshift()

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

How would you insert an item called “item” to the 5th position of an array called my_array?

A

my_array.insert(4, “item”)

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

Who would you remove redundancies in an array called my_array without destroying the original array?

A

new_array = my_array.uniq

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

Who would you remove redundancies in an array called my_array

A

my_array.uniq!

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

How would you check if an array called my_array have an item called “my item” in it?

A

my_array.include?(“my item”)

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

Who do you retrieve and remove the first item of an array called my_array?

A

first_item = my_array.shift

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

Who do you retrieve the first item of an array called my_array without removing it from the array?

A

fist_item = my_array[0]

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

Who do you retrieve and remove the last item of an array called my_array?

A

last_item = my_array.pop

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

Who do you retrieve the last item of an array called my_array without removing it from the array?

A

last_item = my_array[-1]

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

Who do you retrieve the number of items in an array?

A

my_array.length

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

Who do you delete all the items in an array

A

my_array.clear

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

Who would you convert a string stored in the variable ‘var’ to all cups?

A

var.upcase

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

What method do you use in order to remove the \n character from a string entered to the gets method?

A

.chomp

20
Q

How do you convert a string stored in the variable ‘var’ to an integer?

A

var.to_i

21
Q

What is the shorthand l to creating this array:

a = [‘this’, ‘that’, ‘and’]

A

a = %{ this that and }