To declare a variable, you must use the =
assignment operator.
In Crystal variables should be written in snake_case.
number = 1
puts number # => 1
number = 2
puts number # => 2
Constants are declared using the same =
assignment operator, but use all uppercase letters, also known as SCREAMING_SNAKE_CASE.
NUMBER = 1
puts NUMBER # => 1
Assigning a value to a constant that is already defined will result in a compile-time error.
NUMBER = 1
NUMBER = 2
# Error: already initialized constant NUMBER
Crystal is a compiled language, which means that every variable or method argument is assigned a type at compile time. The compiler is capable of inferring the type of a variable or method argument. Although in some cases it is necessary to specify the type. The cases that requires a type to be set will be explained in later concepts.
Methods are a way to group together a set of instructions that can be reused.
In Crystal methods are often defined in a class, module or struct.
But methods can also be defined outside of a class, module or struct.
Methods are declared using the def
keyword, followed by the name of the method.
In Crystal methods should be written in snake_case.
When a method doesn't have any arguments, you can omit the parentheses.
To declare the end of a method, you must use the end
keyword.
def hello
puts "Hello World!"
end
Using a method that doesn't exist on the type of the variable or method argument will result in a compile-time error.
number = 1
number + "1"
In test.cr:2:7
2 | number + "1"
^
Error: no overload matches 'Int32#+' with type String
Methods can have arguments. Arguments are data that is passed to a method. To be able to give a method arguments, you must specify the name of the argument. In Crystal arguments should be written in snake_case. A method can have multiple arguments, but the arguments must be separated by a comma.
def hello(name)
puts "Hello #{name}!"
end
Methods can also have multiple arguments. In this case, the arguments must be separated by a comma.
def hello(name, language)
puts "Hello #{name}! You are learning #{language}."
end
When calling a method that belongs to a class, module or struct, you must use the dot operator(.
).
Like following: <ClassName>.<method_name>
.
When the method doesn't belong to a class, module or struct, then you can simple call it by writing its name.
Methods always implicitly return the value of the last expression in the method.
When a method has arguments, you may use parentheses when specifying the arguments in the method call and definition, like following: <method_name>(<argument_1>, <argument_2>)
.
When a method has no arguments, parentheses should be omitted.
def hello(name)
"Hello #{name}!"
end
hello("Crystal")
# => Hello Crystal!
When calling a method with multiple arguments, the arguments should be separated by a comma. The arguments must be given in the same order as the method arguments.
def hello_language(name, language)
puts "Hello #{name}! You are learning #{language}."
end
hello_language("World", "Crystal")
# => Hello World! You are learning Crystal.
Calling a method with the wrong number of arguments will result in a compile-time error.
hello_language("Crystal")
In test.cr:1:1
1 | hello_language("Crystal")
^----
Error: wrong number of arguments for 'hello_language' (given 1, expected 2)
In Crystal, you can use the +
operator to add two numbers together.
You can also use the -
operator to subtract two numbers.
And you can use the *
operator to multiply two numbers.
1 + 1
# => 2
2 - 1
# => 1
2 * 2
# => 4
In Crystal comments are written with the #
character, followed by a space and then the comment.
Comments are used to document code.
Comments are not executed by the compiler.
# This is a comment
In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
You have four tasks, all related to the time spent cooking the lasagna.
Define the EXPECTED_MINUTES_IN_OVEN
constant in the Lasagna
class, that stores how many minutes the lasagna should be in the oven.
According to the cooking book, the expected oven time in minutes is 40:
Lasagna::EXPECTED_MINUTES_IN_OVEN
# => 40
Define the Lasagna#remaining_minutes_in_oven
method that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.
Lasagna.new.remaining_minutes_in_oven(30)
# => 10
Define the Lasagna#preparation_time_in_minutes
method that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
Lasagna.new.preparation_time_in_minutes(2)
# => 4
Define the Lasagna#total_time_in_minutes
method that takes two named parameters: the number_of_layers
parameter is the number of layers you added to the lasagna, and the actual_minutes_in_oven
parameter is the number of minutes the lasagna has been in the oven.
The method should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
number_of_layers = 3
actual_minutes_in_oven = 20
Lasagna.new.total_time_in_minutes(number_of_layers, actual_minutes_in_oven)
# => 26
Sign up to Exercism to learn and master Crystal with 21 concepts, 126 exercises, and real human mentoring, all for free.