Intro

Crystal is a object orientated language. Classes are the blueprints from which objects can be created.

Class Definition

crystal
# 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

Class methods allow you to add behaviour to a class.

crystal
# 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

Variable Access

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.

crystal
# 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

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.

crystal
# 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

Class inheritance allows the properties of a superclass to be shared with its subclasses. Class inheritance is defined with the subclass < superclass syntax.

crystal
# 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

Abstract Class

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.

crystal
# 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

Considerations

  • Classes are defined in CamelCase by convention.
  • When you create a class , you are also defining a new type .
  • By default class methods are public and visible from outside the class.
  • A class methods visibility can be changed to either private or protected.
  • Classes are created on the heap and are passed by reference.