Ruby - Week 3 Flashcards

1
Q

In references, what does the = sign do even when syntactic sugar is being used? -= or +=

A

”=” operator will always reassign and create a NEW pointer in memory (the arrows)

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

When x = 4 and y = x and x += 2, what is y?

A

y will remain at 4 because x += 2 expands to x + 2 = 6 which is points to another point in memory.

FIXED NUMs are immutable objects. The numbers are already stored in memory therefore we cannot change them.

Integers and floats are immutable. they just provide new values.

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

What are MUTABLE objects?

  1. If x = “Donatello” and y = x and x &laquo_space;“TMNT” what would y be?
  2. If x = “Donatello” and y = x and x += “TMNT” what would y be?
A

Strings, arrays, hashes whether using the shovel operator. When you use the += or -= operators IT WILL create a new space in memory even tho its mutable.

One point is that = does not mutate (modify) an object; it merely reassigns the variable so that it now refers to a new object.

  1. y would be “Donatello TMNT” and be identical to X since we’re mutating
  2. In this example, we’re using the “=” assignment operator so its creating a new place in memory and y = Donatello and X = Donatello TMNT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does the “=” mutate or reassign and hash?

A

“hash[key] = value” DOES NOT reassign the hash it’s only mutating the key value pairs..

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

What does the | | operation do?

A

Ruby is “lazy” and will not evaluate the right side of the or operator if the left side is already true. It’ll pull the first “truthy” value. it knows that regardless of what’s on the right side, if the left and first value is a truthy, then the whole statement will be true.

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

What is the Single Responsibility Principle?

A

It’s the principle that methods should do a single thing at a time and should be short. They should be atomic.

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

What is a rule of thumb for referencing variables in methods?

A

Should only reference variables that are passed in as arguments and avoid using any global variables

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

What is code smell?

A

Not necessarily a bug but indicate weakness in the code design that could possibly lead to bugs down the line or slow down development.

Examples of smells:
1. Duplicated or similar code that doesn’t apply the DRY rule

  1. Long methods that aren’t broken down into smaller ones that are atomic
  2. too many parameters
  3. Data clump = data can be grouped more easily like start date and end date being merged to just date range
  4. Indecent exposure = classes should have bare minimum interaction with outside world and should be hidden typically with minimized exposure
  5. God object = don’t create a single master object that is tightly connected to all the objects. Good OO design results in classes that are lightly coupled (dependency)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly