Programming Crystal

Book description

Crystal is for Ruby programmers who want more performance, or for developers who enjoy working in a high-level scripting environment. Crystal combines native execution speed and concurrency with Ruby-like syntax, so you will feel right at home. This book, the first available on Crystal, shows you how to write applications that have the beauty and elegance of a modern language, combined with the power of types and modern concurrency tooling. Now you can write beautiful code that runs faster, scales better, and is a breeze to deploy.

Crystal is elegant to read and easy to program like Ruby, allowing full object-oriented development. Its compiler is powerful enough to nearly always infer the type of your variables. So you get the benefits of a statically typed language: more robust code, safety and execution speed, while still reaching high productivity in development. Null pointer exceptions as in JavaScript, Java or C#, are a thing of the past: Crystal annihilates them, just like Rust.

Explore the building blocks and design of the language, and how you can use the Crystal tool-chain to build and manage powerful applications. Harness the power of the macro system, as well as how to work with fibers and channels, making concurrency as easy as possible. Learn how to use the Kemal web framework and access databases, and how to tap the potential of existing Crystal libraries. Find the spot that Crystal fills in today's software world with real-world examples.

With Crystal, you can combine the best of both worlds: the high-level coding of dynamic languages, and the safety and blazing performance of a natively compiled language.

What You Need:

To develop in Crystal, you only need Crystal v 0.26 the latest version, a common text editor and a browser.

Publisher resources

View/Submit Errata

Table of contents

  1.  Preface
    1. Who This Book Is For
    2. What’s in This Book
    3. How to Read This Book
    4. Conventions Used in This Book
    5. Web Resources and Feedback
    6. Downloading Sample Code
    7. Acknowledgments
  2. Part I. Getting Started
    1. 1. Diving into Crystal
      1. A Programming Language for Humans and Computers
      2. Slick As Ruby, But Way Faster
      3. Almost As Fast As C
      4. Speeding Up the Web
      5. Talking to Databases
      6. More Safety Through Types
      7. No to the Billion-Dollar Mistake
      8. Batteries Included
      9. Putting Crystal to Good Use
      10. A Company’s Story Crystallized: Red Panthers
      11. Wrapping Up
    2. 2. Crystal Foundations
      1. Using Basic Variables and Types
      2. Variable Operations
      3. Structuring Data with Container Types
      4. Controlling the Flow
      5. Using Methods
      6. Organizing Code in Classes and Modules
      7. Executing Code Concurrently Through Fibers
      8. A Company’s Story Crystallized: Dev Demand
      9. Wrapping Up
  3. Part II. Building Blocks
    1. 3. Typing Variables and Controlling the Flow
      1. Converting Data Between Types
      2. Getting Input
      3. Putting It Together—Converting Currencies 1
      4. Exception Handling for Faulty Input
      5. Chaining Methods
      6. Getting Input from Command-Line Arguments
      7. Using String Methods
      8. Using Symbols as Identifiers
      9. Using Enums
      10. Using Regular Expressions
      11. Putting It Together—Converting Currencies 2
      12. Beyond Hashes and Arrays: More Composite Types
      13. Nilable Types
      14. Controlling the Flow and Types
      15. A Company’s Story Crystallized: Linkfeed
      16. Wrapping Up
    2. 4. Organizing Code in Methods and Procs
      1. Passing Arguments
      2. Returning Values
      3. Working with Yield, Procs, and Blocks
      4. Overloading and Multiple Dispatch
      5. Using a Shorter Syntax for Exception Handling
      6. Using Recursive Methods
      7. A Company’s Story Crystallized: Duo Design
      8. Wrapping Up
    3. 5. Using Classes and Structs
      1. Converting a Ruby Class to Crystal
      2. Structuring a Class
      3. Applying Inheritance
      4. Controlling Visibility
      5. Working with Structs
      6. Viewing the Type Hierarchy
      7. Some Nice Tricks
      8. A Company’s Story Crystallized: LI-COR Biosciences
      9. Wrapping Up
    4. 6. Working with Modules
      1. Combining Files with Require
      2. Using Modules as Namespaces
      3. Letting Modules Extend Themselves
      4. Mixing in Modules
      5. How the Compiler Finds Methods
      6. Applying Built-In Modules
      7. A Company’s Story Crystallized: Diploid
      8. Wrapping Up
    5. 7. Managing Projects
      1. Creating a Shard
      2. Formatting Code
      3. Documenting a Project
      4. Writing Tests with Spec
      5. Using External Libraries
      6. Benchmarking Your Code
      7. Deploying a Crystal App
      8. A Company’s Story Crystallized: Diploid—Part 2
      9. Wrapping Up
  4. Part III. Advanced Crystal
    1. 8. Advanced Features
      1. DRY Your Code with Macros
      2. Low-Level Programming and C Bindings
      3. Creating Concurrent Code
      4. Accessing Databases
      5. A Company’s Story Crystallized: NeuraLegion
      6. Wrapping Up
    2. 9. Using Web Frameworks and Shards
      1. Build Web Applications with the Kemal and Amber Frameworks
      2. A Brief Tour of the Shard Ecosystem
      3. A Company’s Story Crystallized: Kemal in Production
      4. Wrapping Up and Afterword
  5. Part IV. Appendices
    1. A1. Setting Up a Crystal Environment
      1. Working with Crystal Online
      2. Installing Crystal on Your Machine
      3. Compiling Code
      4. Using Editors and IDEs
      5. Working with Crystal Playground
      6. Using Crystal Documentation
      7. Wrapping Up
    2. A2. Porting Ruby Code to Crystal
      1. Ruby Extensions in Crystal
    3. A3. Your Turn Answers
      1. Chapter 2: Crystal Foundations
      2. Chapter 3: Typing Variables and Controlling the Flow
      3. Chapter 4: Organizing Code in Methods and Procs
      4. Chapter 5: Using Classes and Structs
      5. Chapter 6: Working with Modules
      6. Chapter 7: Managing Projects
      7. Chapter 8: Advanced Features
      8. Chapter 9: Web Frameworks and the Shard Ecosystem
      9. Appendix 1: Setting Up a Crystal Environment

Product information

  • Title: Programming Crystal
  • Author(s): Ivo Balbaert, Simon St. Laurent
  • Release date: January 2019
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680502862