Methods Flashcards

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

How to delete a element from the end of an array?

A

arr = [1, 2, 3]
arr.pop
=> arr = [1, 2]
POP!!!

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

How to delete an element from the beginning of the array

A

arr = [1, 2, 3]
arr.shift
=> arr = [ 2, 3 ]
(Will return the value that was deleted in the terminal)

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

How to Delete an element at a given position

A

arr = [1, 2, 3]
arr.delete_at(1)
=> arr = [ 1, 3 ]

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

How to Delete all occurrences of a given element

A

arr = [1, 2, 3, 5, 6, 8, 5]

arr.delete(5)

=> [1, 2, 3, 6, 8]

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

How to select elements that satisfy a given criteria?

A

arr = [ 3, 4, 2, 1, 2, 3, 4, 5, 6]
arr.select {|a| a > 2}
[3, 4, 3, 4, 5, 6]

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

What is non destructive selection?

A

It is when the arrays remain unchanged after the selection.

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

How do you permanently delete objects in an array?

A

you use the delete_if method

arr.delete_if { | a | a < value }

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

How do you keep some of the objects in an array?

A

You use the .keep_if method

arr.keep_if {|a| a < value }

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

What is a splat operator?

A

A Splat operator gives us the method to insert multiple arguments, without predefining them.

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

What is a keyword based splat argument?

A

It is a mix between a keyword argument and a splat arguments.

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

What are optional arguments?

A

Optional arguments allows you to pass in any kind of arguments and then utilize them. They can lead to misleading bugs.

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