We're going to start by installing Homebrew which will make installing everything else we need much easier.
Open a Terminal window and run the following commands:
1 2 xcode-select --install /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now we can easily install Crystal with Homebrew using the following:
1 brew install crystal-lang
Now we'll use crystal to initialize a project for us.
1 2 3 # crystal is installed with the language crystal init lib ystalcray cd ystalcray
When building software you will frequently use libraries and tools from a third party. Since they have their own development lifecycles and versions we want to be able to define a specific version of that dependency to use in our software. We want to do that with a tool and a file.
For Crystal we'll use the tool crystal and our dependencies will be defined in the file
1 # crystal is installed with the language
We're going to use the testing library Spec to write our tests.
Edit the file
shard.yml and add Spec as a testing dependency:
Then we retrieve Spec by running
crystal deps install.
Finally we initialize Spec by running
Create the file
spec/ystalcray.cr with the following contents:
1 2 3 4 5 6 7 8 9 require "./spec_helper" describe Ystalcray do describe "translate_word" do it "translates words starting with a vowel" do Ystalcray.translate_word("apple").should eq "appleway" end end end
Then run the test with the command
crystal spec to see the results:
1 2 3 4 5 6 Error in line 1: while requiring "./spec/ystalcray_spec.cr" in spec/ystalcray_spec.cr:6: undefined method 'translate_word' for Ystalcray:Module Ystalcray.translate_word("apple").should eq "appleway" ^~~~~~~~~~~~~~