Ruby - Week 3 Flashcards
In references, what does the = sign do even when syntactic sugar is being used? -= or +=
”=” operator will always reassign and create a NEW pointer in memory (the arrows)
When x = 4 and y = x and x += 2, what is y?
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.
What are MUTABLE objects?
- If x = “Donatello” and y = x and x «_space;“TMNT” what would y be?
- If x = “Donatello” and y = x and x += “TMNT” what would y be?
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.
- y would be “Donatello TMNT” and be identical to X since we’re mutating
- In this example, we’re using the “=” assignment operator so its creating a new place in memory and y = Donatello and X = Donatello TMNT
Does the “=” mutate or reassign and hash?
“hash[key] = value” DOES NOT reassign the hash it’s only mutating the key value pairs..
What does the | | operation do?
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.
What is the Single Responsibility Principle?
It’s the principle that methods should do a single thing at a time and should be short. They should be atomic.
What is a rule of thumb for referencing variables in methods?
Should only reference variables that are passed in as arguments and avoid using any global variables
What is code smell?
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
- Long methods that aren’t broken down into smaller ones that are atomic
- too many parameters
- Data clump = data can be grouped more easily like start date and end date being merged to just date range
- Indecent exposure = classes should have bare minimum interaction with outside world and should be hidden typically with minimized exposure
- 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)