Using External Libraries

When building your app, you don’t want to reinvent the wheel. More often than not, parts of your functionality are already covered by existing Crystal packages or libraries. Commonly called shards, they’re the equivalent of gems in the Ruby world.

shards is also the name of the application that manages the dependencies of a project, like bundler in Ruby. How can you instruct your app to load one or more external shards?

Your app can list its dependencies in the file shard.yml (for Rubyists: the gemfile), which we’ll take a closer look at now. At the start of a project, it looks like this:

 name: ​mineral
 version: ​0.1.0
 
 authors:
  ​- Your-Name <your-email-address>
 
 targets:
  mineral:
  main: ​src/mineral.​ ...

Get Programming Crystal now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.