Today I spent a few hours playing around with the Crystal language. For a project that’s considered “new” the docs are pretty great. Coming from the world of Ruby writing basic Crystal code is straightforward and most of everything works as expected.
For fun and science I decided to try and throw together a simple program that would allow me to try the following:
- Flesh out some basic classes
- Raise exceptions
- Use public/private methods
- Import code
What I came up with is a pointless ~30 line program that takes a word, looks up the Wikipedia page for that word and finally prints the page’s title. The code is extremely easy to read and while putting it together I found that most things just “worked” as they would in Ruby.
The only gotcha I found was that when extracting text from the page body’s title a MatchData object is returned and the matches are accessed via a block. Ruby also has MatchData . Unlike Crystal, Ruby’s implementation provides MatchData#captures for a super easy way to access an array of the actual matches… which is what you probably care about.
Here’s the code: