Arrays Flashcards
How can you create arrays?
How do you delete values from arrays?
With the delete method
array.delete(value)
How do you delete an element from an array using the index?
With the delete at method
array.delete_at(index_number)
How do you delete elements from array that fit into an criteria?
with the delete if method
array.delete_if { |x| some_code }
What is the join method?
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”
What is the pop method?
It allows you to delete the last element of an array
1) array.pop
What is the push method?
it allows you to add an element to end of an array
1) array.push(element)
2 array.push(element1, element2, element3)
What is an hash?
The hash is an key:value collection that can store values.
How do you create a new hash?
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 do you grab an element from an hash?
hash{:chosen_key]
How do you delete elements from a hash?
you use the delete method
hash.delete(:key)
How do you iterate over just a hash key?
With the each_key method
hash.each_key do |key|
some_code
end
How do you iterate over just a hash value?
With the each_value method
hash.each_value do |value|
some_code
end
How do you add to a hash?
hash[:key] = value
How do you invert the key and the values in a hash?
You use the .invert method
hash.invert