Levels of Abstraction
In the RubyMonstas course we will learn about 3 levels of abstraction:
# | Concept | Deals with... |
---|---|---|
1 | Variables | Data |
2 | Methods | Behaviour |
3 | Classes | Data + Behaviour |
This slideset will show the first level of abstraction, variables.
"The psychological profiling [of a programmer] is mostly the ability to shift levels of abstraction, from low level to high level. To see something in the small and to see something in the large."
Donald Knuth - Computer Scientist Extraordinaire
Before variables, we were only dealing with concrete values, such as:
7
"Hello"
3.1415926
With variables we've reached the first level of abstraction.
Instead of using concrete values, we can now use placeholders that stand in for those values:
number_of_continents = 7
greeting = "Hello"
pi = 3.1415926
Thus, abstraction allows us to step away from concrete things towards broader concepts.
It also allows us to give things names (such as the variable names here).
This gives us the tools to express more complex things.
Abstraction is about reusing code and showing intent:
puts [342.75, 108.20, 241.00, 632.15, 499.55].sum
vs.
monthly_expenses = [342.75, 108.20, 241.00, 632.15, 499.55]
puts monthly_expenses.sum
# | Concept | Deals with... | |
---|---|---|---|
1 | Variables | Data | ✅ |
2 | Methods | Behaviour | |
3 | Classes | Data + Behaviour |
What questions do you have?