Ruby Pro Tip: Short Circuit Assignment
How not to conditionally set a variable:
Instead, you should use the ||= operator:
Both code blocks will do the same, but the second one is cleaner and way more readable. You might also at some point need to evaluate if B is nil and assign C to A if so, this is how not to do it:
One could argue in favor of using a ternary operator instead, which is better but not ideal either:
We can further simplify this expression by using the || operator instead:
Now that’s how the cool kids do it!