w1d1_revisions Flashcards
The following notation in Ruby is invalid, because commas have a separate meaning:
1,000,000
What is an alternate and correct way to notate the comma?
With an underscore (‘_’):
1_000_000
T/F: When handling cases where an input will return an exceptional/unique value, it is better to deal with that in the beginning of the function.
T
When should you use Array#take_while ?
When you want all the elements of an array UP TO a certain point.
If you find yourself using an if statement where you are checking for a negation, what is a more elegant way to write the solution?
Use ‘unless’:
def my_uniq(array) uniq_array = []
array.each do |el|
uniq_array «_space;el unless uniq_array.include?(el)
end
7
8 uniq_array
9 end
What does Integer#upto do, and when should it be used?
It iterates (+=1 each iteration) over the given block until it hits the limit given (the argument).
It should be used when you want to do something to a number while it is increasing:
5.upto(10) { |i| print i, " " } #=> 5 6 7 8 9 10
What should you do when you have an unnecessary iteration?
Use the ‘next’ keyword
T/F: The following syntax is invalid:
next if sell_date < buy_date
F; It is valid b/c ‘next if’ can be used as a compound keyword without an ‘end’ keyword
What is an extremely efficient way to create an array of incremental values?
(rangeStart..rangeEnd).to_a
Explain the following code:
stack_num = move_hash[gets.chomp]
Sets ‘stack_num’ to the value of the key the in move_hash, which the user enters with ‘gets.chomp’
Explain Hash#values_at
Takes an argument (an series of keys), and returns, in the order the keys are given, an array of the values associated with the given keys.