January 2019
Intermediate to advanced
246 pages
5h 23m
English
In the previous section, and in Organizing Code in Classes and Modules, you saw a simple Mineral class. Here’s the class code by itself without any additional logic:
| | class Mineral |
| | getter name : String |
| | getter hardness : Float64 |
| | getter crystal_struct : String |
| | |
| | def initialize(@name, @hardness, @crystal_struct) # constructor |
| | end |
| | end |
This class has three read-only instance variables: name, hardness, and crystal_struct. Giving them a type is imposed by the Crystal compiler. But you can also do this in the initialize method:
| | class Mineral |
| | getter name, hardness, crystal_struct |
| | |
| | def initialize(@name : String, |
| | @hardness : Float64, |
| | @crystal_struct : String) |
| |
Read now
Unlock full access