Methods Flashcards
What is the formal way to declare an array?
my_array = Array.new
What is the literal way to declare an empty array
my_array = []
What are two ways to add a new item called “new item” to the end of an array called “my_array”
my_array.push(“new item”)
and
my_array «_space;“new item”
How would you add an item to the beginning of an array
my_array.unshift()
How would you insert an item called “item” to the 5th position of an array called my_array?
my_array.insert(4, “item”)
Who would you remove redundancies in an array called my_array without destroying the original array?
new_array = my_array.uniq
Who would you remove redundancies in an array called my_array
my_array.uniq!
How would you check if an array called my_array have an item called “my item” in it?
my_array.include?(“my item”)
Who do you retrieve and remove the first item of an array called my_array?
first_item = my_array.shift
Who do you retrieve the first item of an array called my_array without removing it from the array?
fist_item = my_array[0]
Who do you retrieve and remove the last item of an array called my_array?
last_item = my_array.pop
Who do you retrieve the last item of an array called my_array without removing it from the array?
last_item = my_array[-1]
Who do you retrieve the number of items in an array?
my_array.length
Who do you delete all the items in an array
my_array.clear
Who would you convert a string stored in the variable ‘var’ to all cups?
var.upcase