Crystal Programming

Book description

The ultimate guide to Crystal programming covering all its fundamental concepts such as OOP and concurrent programming to help you write readable and safe code and build fast applications

Key Features

  • The book uses an example-based approach for a better demonstration of the underlying concepts
  • Develop a thorough appreciation of the roles of the macro API and annotations
  • Leverage supportive tools — spec, documentation, deployment, and automation

Book Description

Crystal is a programming language with a concise and user-friendly syntax, along with a seamless system and a performant core, reaching C-like speed. This book will help you gain a deep understanding of the fundamental concepts of Crystal and show you how to apply them to create various types of applications.

This book comes packed with step-by-step explanations of essential concepts and practical examples. You'll learn how to use Crystal's features to create complex and organized projects relying on OOP and its most common design patterns. As you progress, you'll gain a solid understanding of both the basic and advanced features of Crystal. This will enable you to build any application, including command-line interface (CLI) programs and web applications using IOs, concurrency and C bindings, HTTP servers, and the JSON API.

By the end of this programming book, you'll be equipped with the skills you need to use Crystal programming for building and understanding any application you come across.

What you will learn

  • Explore how Crystal combines the merits of other languages
  • Understand how to leverage existing C libraries without writing any C
  • Focus on zero-cost abstractions with compile-time macros
  • Use an example-based approach to demonstrate language features
  • Develop a variety of Crystal applications, such as web and CLI apps
  • Gain an understanding of the macro API and annotations

Who this book is for

Developers who want to learn Crystal programming or anyone looking to improve their ability to solve real-world problems using the language will find this book helpful. Experience in application development using any other programming language is expected. However, prior knowledge of Crystal is not required.

Table of contents

  1. Crystal Programming
  2. Contributors
  3. About the authors
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1: Getting Started
  7. Chapter 1: An Introduction to Crystal
    1. Technical requirements
    2. A bit of history
    3. Exploring Crystal's expressiveness
    4. Crystal programs are also FAST
      1. A web server comparison
    5. Setting up the environment
    6. Creating our first program
      1. Creating an executable
    7. Summary
  8. Chapter 2: Basic Semantics and Features of Crystal
    1. Technical requirements
    2. Values and expressions
      1. Numbers
      2. The primitive constants – true, false, and nil
      3. String and Char
      4. Ranges
      5. Enums and symbols
    3. Controlling the execution flow with conditionals
      1. if and unless
      2. case
      3. while and until loops
    4. Exploring the type system
      1. Experimenting with the crystal play command
    5. Organizing your code in methods
      1. Adding type restrictions
      2. Default values
      3. Named parameters
      4. External and internal names for parameters
      5. Passing blocks to methods
    6. Data containers
      1. Arrays and tuples
      2. Hash
      3. Iterating collections with blocks
      4. Short block syntax
      5. Splat parameters
    7. Organizing your code in files
      1. require "./filename"
      2. require "filename"
    8. Summary
    9. Further reading
  9. Chapter 3: Object-Oriented Programming
    1. Technical requirements
    2. The concept of objects and classes
    3. Creating your own classes
      1. Manipulating data using instance variables and methods
      2. Creating getters and setters
      3. Inheritance
      4. Polymorphism
      5. Abstract classes
      6. Class variables and class methods
    4. Working with modules
    5. Values and references – using structs
    6. Generic classes
    7. Exceptions
      1. Custom exceptions
    8. Summary
  10. Part 2: Learning by Doing – CLI
  11. Chapter 4: Exploring Crystal via Writing a Command-Line Interface
    1. Technical requirements
    2. Project introduction
    3. Scaffolding the project
    4. Writing the basic implementation
      1. Transforming the data
      2. Improving reusability
    5. Summary
  12. Chapter 5: Input/Output Operations
    1. Technical requirements
    2. Supporting terminal input/output
    3. Supporting other IO
    4. Performance testing
    5. Explaining IO behavior
    6. Summary
  13. Chapter 6: Concurrency
    1. Technical requirements
    2. Using fibers to complete work concurrently
    3. Using channels to communicate data safely
    4. Transforming multiple files concurrently
    5. Summary
  14. Chapter 7: C Interoperability
    1. Technical requirements
    2. Introducing C bindings
    3. Binding libnotify
      1. Testing the bindings
      2. Abstracting the bindings
    4. Integrating the bindings
    5. Summary
  15. Part 3: Learn by Doing – Web Application
  16. Chapter 8: Using External Libraries
    1. Technical requirements
    2. Using Crystal Shards
      1. Shard dependencies on C code
      2. Updating Shards
      3. Checking dependencies
    3. Finding Shards
      1. Example scenario
    4. Summary
  17. Chapter 9: Creating a Web Application with Athena
    1. Technical requirements
    2. Understanding Athena's architecture
    3. Getting started with Athena
      1. The Article entity
      2. Returning an article
      3. Handling the request body
      4. Validation
    4. Implementing database interactions
      1. Setting up the database
      2. Persisting articles
      3. Fetching articles
      4. Updating an article
      5. Deleting an article
    5. Leveraging content negotiation
    6. Summary
    7. Further reading
  18. Part 4: Metaprogramming
  19. Chapter 10: Working with Macros
    1. Technical requirements
    2. Defining macros
      1. Fresh variables
      2. Non-macro definition macros
    3. Understanding the macro API
      1. Recreating the property macro
    4. Exploring macro hooks
    5. Summary
  20. Chapter 11: Introducing Annotations
    1. Technical requirements
    2. What are annotations?
    3. Storing data within annotations
    4. Reading annotations
    5. Summary
  21. Chapter 12: Leveraging Compile-Time Type Introspection
    1. Technical requirements
    2. Iterating type variables
    3. Iterating types
      1. Iterating a type's subclasses
      2. Iterating types with a specific annotation
      3. Iterating types that include a specific module
    4. Iterating methods
    5. Summary
    6. Further reading
  22. Chapter 13: Advanced Macro Usages
    1. Technical requirements
    2. Using annotations to influence runtime logic
    3. Exposing compile-time data at runtime
      1. Accessing the value
      2. Modeling an entire class
    4. Determining a constant's value at compile time
    5. Creating custom compile-time errors
      1. Restricting generic types
    6. Summary
  23. Part 5: Supporting Tools
  24. Chapter 14: Testing
    1. Technical requirements
    2. Why test?
    3. Unit testing
      1. Tagging tests
      2. Mocking
      3. Hooks
    4. Integration testing
    5. Summary
  25. Chapter 15: Documenting Code
    1. Technical requirements
    2. Documenting Crystal code
      1. Linking an API feature
      2. Formatting
    3. Documentation directives
      1. Ditto
      2. Nodoc
      3. Inherit
    4. Generating the documentation
      1. Hosting the documentation
      2. Documentation versioning
    5. Summary
  26. Chapter 16: Deploying Code
    1. Technical requirements
    2. Versioning your shard
    3. Creating production binaries
    4. Distributing your binary
      1. Via Docker
      2. Via package manager(s)
    5. Summary
    6. Further reading
  27. Chapter 17: Automation
    1. Technical requirements
    2. Formatting code
    3. Linting code
    4. Continuous integration with GitHub Actions
      1. Formatting, coding standards, and tests
      2. Deploying documentation
    5. Summary
  28. Appendix A: Tooling Setup
    1. Installing the Crystal compiler
      1. Installing the compiler on Windows
    2. Installing Visual Studio Code
  29. Appendix B: The Future of Crystal
    1. Windows
    2. WebAssembly
    3. Multithreading
    4. Structured concurrency
    5. Incremental compilation and better tooling
    6. How to get in touch with the community
    7. Why subscribe?
  30. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Crystal Programming
  • Author(s): George Dietrich, Guilherme Bernal
  • Release date: May 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801818674