Ruby Intro/Flow Control Flashcards

1
Q

What type of operator is this?

2 * 3 = 6 true ? puts {“Hello”) : puts (“Goodbye”)

A

a ternary operator. these let you make short “if” statements on one line

if the expression before ? is true, the first string is printed. if the expression is false, the second string is printed

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

Basically, what do conditionals do?

A

control the flow of data

control which path(s) the data will take in the program

“forks in the road”

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

1:

Which example shows the correct order?

if
elsif
else
end
-----------------
#2:

if
else
elsif
end

A

1 is correct

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

Is this formatting correct?

if a == 3
puts “a is 3”
else
puts “a is neither 3, nor 4”

A

No. the “puts” should be indented

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

When putting an “if” statement on just one line, which word must you include?

A

then

if x == 3 then puts “good” end

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

Which one is correct?

puts “good” if x == 3

if x=3 then puts “good” end

A

they are both possible

ruby allows both forms

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

!=

These are some comparison operators:

<=
>

What type of value do comparison operators always return?

A

a boolean value

true/false

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

True or false?

(3 * 6) == “eighteen”

A

false

the two values being compared must be of the same type

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

Why is this true?

irb :006 > “42” > “402”
=> true

A

because ruby does a character-by-character comparison (moving left to right):

4 and 4 = same
2 and 0 = not the same, and 2 is greater
(the comparison stops here)

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

What do these let you do?

&&

|

A

combine conditional expressions to make a larger expression

(3 > 2 == false) && (true == true)
false

(BOTH EXPRESSIONS TO THE LEFT AND RIGHT OF && MUST BE TRUE FOR THE ENTIRE EXPRESSION TO BE TRUE)

2 + 4 = 7 | | false == false
true

(ONLY ONE EXPRESSION NEEDS TO BE TRUE FOR THE ENTIRE EXPRESSION TO BE TRUE)

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