updated: 4th of August 2021
published: 30th of July 2021
Crystal is a object orientated language. Classes are the blueprints from which objects can be created.
# Define a class.
class Stuff
end
# Create an instance of the Stuff class.
stuff = Stuff.new()
# Check the type of "stuff"
puts typeof(stuff) # => Stuff
Class methods allow you to add behaviour to a class.
# Define a class with a show method.
class StuffAndThings
# Define instance variables and their types.
@stuff : String
@things : String
@blah : String
def initialize(@stuff, @things, @blah)
# Do something with variables on object initialization.
end
# Add a show method to the StuffAndThings class
def show
puts "Stuff: #{@stuff}"
puts "Things: #{@things}"
puts "Blah: #{@blah}"
end
end
# Create an instance of the StuffAndThings class.
stuff_and_things = StuffAndThings.new(
stuff: "my stuff",
things: "my things",
blah: "blah blah"
)
# Use the show method.
stuff_and_things.show
By default, class variables are not accessible from outside the class. You cannot get or set them. Crystal has some conveniences to help with this.
# Define a class.
class StuffAndThings
# Define instance variables and their types.
@stuff : String
@things : String
@blah : String
# Set variables read/write properties.
getter stuff # The stuff variable can be read but not written.
setter things # The things variable can be written but not read.
property blah # The blah variable can be read and written.
def initialize(@stuff, @things, @blah)
# Do something with variables on object initialization.
end
end
Class variables are prefixed with a double (@@) symbol. Each object created from a class shares the class variables. Sub-classes get their own copy with the value shared across subclasses.
# Define a class with class variables.
class Blah
@@blah = "blah blah blah"
def blah
@@blah
end
end
# Create a new class.
blah = Blah.new()
# Access the class variable.
puts(blah.blah())
Class inheritance allows the properties of a superclass to be shared with its subclasses. Class inheritance is defined with the subclass < superclass syntax.
# Define a parent class.
class Parent
@greet : String
property greet
def initialize(@greet="hello")
end
end
# Define a child class that inherits from parent.
class Child < Parent
end
# Create an instance of the parent and child classes.
Parent.new().greet # => hello
Child.new().greet # => hello
An abstract class is like an interface in other languages. It is a blueprint that specifes the methods that must be implemented in its derived classes.
# Define an abstract class.
abstract class StuffAndThings
# The stuff and things methods must be implemented
# in any derived classes of StuffAndThings.
abstract def stuff
abstract def things
end
https://pragprog.com/titles/crystal/programming-crystal/
https://crystal-lang.org/reference/syntax_and_semantics/new%2C_initialize_and_allocate.html
https://crystal-lang.org/reference/syntax_and_semantics/class_methods.html
https://crystal-lang.org/reference/syntax_and_semantics/visibility.html
https://crystal-lang.org/reference/syntax_and_semantics/class_variables.html