January 2024
Intermediate to advanced
718 pages
20h 15m
English
When you see a variable in your code, it’s useful to know what values can be assigned to that variable without the code breaking. For example, you might have the following Ruby method:
| | def mystery_method(x) |
| | x * 3 |
| | end |
You’d likely expect that x should be a number. But it’s also completely valid Ruby for x to be a string ("a" * 3 resolves to “aaa”) or an array ([:a] * 3 resolves to [:a, :a, :a]).
Let’s say that this method is in your code, and over time people call mystery_method with strings, integers, floating-point numbers, and so on, until somebody changes the method and inadvertently changes what variables it’ll accept. Here’s an example:
| | def mystery_method(x) |
| | x.abs * 3 |
| | end |
Now all of those string ...
Read now
Unlock full access