w1d1_revisions Flashcards

1
Q

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?

A

With an underscore (‘_’):

1_000_000

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

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.

A

T

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

When should you use Array#take_while ?

A

When you want all the elements of an array UP TO a certain point.

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

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?

A

Use ‘unless’:

def my_uniq(array) 
  uniq_array = [] 

array.each do |el|
uniq_array &laquo_space;el unless uniq_array.include?(el)
end
7

8 uniq_array
9 end

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

What does Integer#upto do, and when should it be used?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What should you do when you have an unnecessary iteration?

A

Use the ‘next’ keyword

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

T/F: The following syntax is invalid:

next if sell_date < buy_date

A

F; It is valid b/c ‘next if’ can be used as a compound keyword without an ‘end’ keyword

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

What is an extremely efficient way to create an array of incremental values?

A

(rangeStart..rangeEnd).to_a

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

Explain the following code:

stack_num = move_hash[gets.chomp]

A

Sets ‘stack_num’ to the value of the key the in move_hash, which the user enters with ‘gets.chomp’

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

Explain Hash#values_at

A

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.

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