Search code examples
crystal-lang

How do I check which Operating System (OS) is used in Crystal?


Is there something similar to Ruby's OS gem which allows me to check if I am running on Mac, Linux or Windows?


Solution

  • This is possible but Crystal is complied, so this can be done at compile time. The internals use flags which seem to be set here.

    {% if flag?(:linux) %}
      # Linux
    {% elsif flag?(:darwin) %}
      # Mac
    {% elsif flag?(:win32) %}
      # Windows
    {% end %}