Methods in Crystal
Crystal supports methods defined on struct types.
To run the program:
In Crystal, classes are used instead of structs for defining custom types with methods. The property
macro is used to create getter and setter methods for the struct fields.
Crystal doesn’t distinguish between value and reference types in method calls. All objects are passed by reference, and there’s no need for explicit pointer dereferencing.
The initialize
method in Crystal is equivalent to a constructor in other languages. It’s called when creating a new instance of the class.
Crystal uses #{}
for string interpolation inside double quotes, which is a more concise way to include variable values in strings compared to some other languages.
Next, we’ll look at Crystal’s mechanism for grouping and naming related sets of methods: modules and mixins.