Flow Control Flashcards

1
Q

If Else

A

if else in ruby works as an expression so you can assign it to a variable

EX

message = if lander_cnt > 10 then "Launching" else "Waiting" end

EX:
Short statements on one line

launch if can_launch?

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

falsy

A

only things that are falsy are nil and literal false

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

“unless” single statement version

A

EX

launch unless feul_level < 25

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

Ternary operator

A

EX

can_launch? ? launch : wait

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

Conditional initialization

A

&&= I don’t remember what this is for

||= Is far more common and people often use this to initialize a variable if it hasn’t already been initialized.

EX

ship ||= Spaceship.new

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

Case statement

A

EX

car_status = case feul_level when "low" then "bad" when "medium" then "fine" when "high" then "great" else "not sure, good luck!"

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

While loop

A

long EX

while high_alert? sound_system.play_siren_cycleend

short EX

while high_alert? do sound_system.play_siren_cycle end

shortest EX

sound_system.play_siren_cycle while high_alert?

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

Until loop

A

inverts the while condition

EX

until ship.at_crusing_velocity? ship.accelerateend

Shortest EX

ship.accelerate until ship.at_cruising_velocity?

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

begin end block

A

Caveat: This code will execute at least once even if your condition is false from the start.

EX

begin lightning.start_flashing sound_system.play_siren_cycleend while hight_alert?begin ship.accelerate make_fake_engine_noiseend until ship.at_cruising_velocity

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

For loop

A

nobody uses it.

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

loop (without block)

A

EX

loop do go_another_light_year puts "this is not the edge of the universe"end

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

upto() & downto() methods

A

EX

20.downto(10) {|i| puts i}

or

10.upto(20) { |i| puts i}

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

.times method

A

EX

3.times {puts "This is serenity, please respond"}

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

step

A
  • Iterates over the range, passing each nth element to the block.
  • If begin and end are numeric, n is added for each iteration.
  • Otherwise, step invokes succ to iterate through range elements.
  • If no block is given, an enumerator is returned instead.

EX1

.step(10,2) { |i| puts i}

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

next

A

next starts the next iteration

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

break

A

terminates the loop

while message = comms.get_message message.process break if message.type == "voice"end

17
Q

loops return values that you can assign to variables

A

EX

text = while message = comms.get_message message.process break message.text if message.type == "voice"end

18
Q

redo

A

repeats iteration

EX

i = 0while i < 3 print "Please enter a positive number: " input = gets.to_i redo if input <= 0 i +=1end

19
Q

exceptions

A
  • Primary way to handle errors in Ruby
  • apply exception to part of a method with a begin end clause. • Must explicitly state a return so it doesn’t go through the rest of the method

EX

def launch begin batten_hatches rescue puts "Couldn't batten the hatches" return false end light_seatbelt_signend

• apply exception to any/all parts of a method by using a rescue clause at the end of a method definition.

EX

def launch batten_hatches light_seatbelt_sign truerescue puts "Exception intercepted" falseend

• if everything goes normally it will return true. otherwise, it will give that error and return false.