January 2019
Intermediate to advanced
246 pages
5h 23m
English
Modules group methods, and then you can use their functionality in different classes. To do that, the module’s code is mixed-in to other types, as in Ruby or Dart. You do this with the keyword include. When a class does an include ModuleName, its objects can use the methods of the included module as their own methods! You saw an example in Organizing Code in Classes and Modules.
Here’s another example: Both classes DVD and BlueRay include the module Debug, so objects made of these classes can use its method who_am_i?
| | class Basic |
| | def initialize(@name : String) |
| | end |
| | |
| | def to_s |
| | @name |
| | end |
| | end |
| | |
| | module Debug |
| | def who_am_i? |
| | "#{self.class.name} ( |
Read now
Unlock full access