Chapter 2 Choose The Right Control Structure Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Use For Loop Or Each?

A

You should use each… B/C ruby defines the for loop in terms of the each method… EX: for font in fonts == fonts.each

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

What’s the difference b/t for loop and each?

A

Any variables that are introduced into a code block are local to that block. Therefore, they disappear at the end of that block. The for loop version does NOT introduce a new scope. Therefore, variables introduced within a for loop are visible outside of the for loop!!!

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

What is a case statement?

A

It’s a giant statement that returns a value. A multi-way decision statement… in other languages it is sometimes called a switch statement. It will return values of the selected when or else clause or.. nil if there are no matches in the case. Case statements are useful for regular expressions. === means absolute

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

In ruby what objects are treated as false?

A

False and nil are the only objects that return a value of false.

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

In ruby what objects return a value of true?

A

In ruby everything is treated as true except for: nil and false. For example number 0 = true.

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

What is the defined? method??

A

-defined?(pass in an argument) Will return a string that describes the argument passed in. EX: topic = Topic.new(title) check = defined?(topic) =>“local variable” If topic was not defined… it would return => nil

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

What is a ternary?

A

A ternary is an expression-based way to make a decision. -?: - file = all ? ‘Specs’ : ‘latest_specs’ It’s like an if statement with the condition right before the ?. Ex: If all is true, then the value of the whole expression is the thing between the ? And : in this case it would be ‘Specs’ otherwise if it’s false it is the thing after the : in this case ‘latest_specs’

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

How and why would you set a variable to a default?

A

If you are unsure whether you need to initialize a variable.

For Example, you might want to ensure that an instance variable IS NOT NIL.

If the variable has a value, leave it… else you should set a default so it IS NOT NIL.

If you are new to ruby you might set a default like this:
@first_name = ‘’ unless @first_name

This works but it should be done as follows:

@first_name ||= ‘’

This is similar to the shorthand syntax

count += 1 which is equivelent to count = count + 1

so…

@first_name ||= ‘’ is equivelent to @first_name = @first_name || ‘’

The short hand is better because it follows DRY

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

Why would you NOT want to use ||= for a default intialization?

A

You should not use the ||= when initializing a boolean..

Why?

B/C if a variable happened to be false it would be set to the default…

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