Methods Flashcards
How to delete a element from the end of an array?
arr = [1, 2, 3]
arr.pop
=> arr = [1, 2]
POP!!!
How to delete an element from the beginning of the array
arr = [1, 2, 3]
arr.shift
=> arr = [ 2, 3 ]
(Will return the value that was deleted in the terminal)
How to Delete an element at a given position
arr = [1, 2, 3]
arr.delete_at(1)
=> arr = [ 1, 3 ]
How to Delete all occurrences of a given element
arr = [1, 2, 3, 5, 6, 8, 5]
arr.delete(5)
=> [1, 2, 3, 6, 8]
How to select elements that satisfy a given criteria?
arr = [ 3, 4, 2, 1, 2, 3, 4, 5, 6]
arr.select {|a| a > 2}
[3, 4, 3, 4, 5, 6]
What is non destructive selection?
It is when the arrays remain unchanged after the selection.
How do you permanently delete objects in an array?
you use the delete_if method
arr.delete_if { | a | a < value }
How do you keep some of the objects in an array?
You use the .keep_if method
arr.keep_if {|a| a < value }
What is a splat operator?
A Splat operator gives us the method to insert multiple arguments, without predefining them.
What is a keyword based splat argument?
It is a mix between a keyword argument and a splat arguments.
What are optional arguments?
Optional arguments allows you to pass in any kind of arguments and then utilize them. They can lead to misleading bugs.