Provides C++-like assert functionality that you can sprinkle throughout your crystal program. Assertions are disabled via macro when the `--release` compile-time flag is specified, allowing performant release builds.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
shard.yml provides a simple C++-style assert! macro you can use anywhere in your crystal code. if the expression passed to assert! roughly evaluates to true at runtime, then nothing will happen and your code will proceed on normally. If the expression evaluates to false (e.g. assert! false, assert! 2 + 1 == 4 or assert! 0), then an exception will be raised giving you a stack trace of the failed assertion as well as a message repeating the expression that failed.

If you build your app/library with the --release compile-time flag (which you should always use for production builds), assertions will not be compiled so there will be zero performance hit for using this library.


Add this to your application's shard.yml:

    github: sam0x17/


This is a simple assertion that will fail if it gets to execute since 5 - 5 = 0 and 0 is false:

require "assert"

assert! 5 - 5
puts "made it past the assertion"

Runtime result without --release flag:

$ crystal run src/
Unhandled exception: assertion "5 - 5" failed (Exception)
  from src/ in '__crystal_main'
  from /usr/share/crystal/src/crystal/ in 'main_user_code'
  from /usr/share/crystal/src/crystal/ in 'main'
  from /usr/share/crystal/src/crystal/ in 'main'
  from __libc_start_main
  from _start
  from ???

Runtime result with --release flag:

$ crystal run src/ --release
made it past the assertion