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