Cover....1
Table of Contents....7
Preface....11
Who This Book Is For....11
What’s in This Book....11
How to Read This Book....13
Conventions Used in This Book....14
Web Resources and Feedback....15
Downloading Sample Code....15
Acknowledgments....16
Part I—Getting Started....17
1. Diving into Crystal....18
A Programming Language for Humans and Computers....18
Slick As Ruby, But Way Faster....19
Almost As Fast As C....22
Speeding Up the Web....24
Talking to Databases....26
More Safety Through Types....27
No to the Billion-Dollar Mistake....28
Batteries Included....29
Putting Crystal to Good Use....30
A Company’s Story Crystallized: Red Panthers....31
Wrapping Up....33
2. Crystal Foundations....34
Using Basic Variables and Types....34
Variable Operations....37
Structuring Data with Container Types....38
Controlling the Flow....43
Using Methods....48
Organizing Code in Classes and Modules....49
Executing Code Concurrently Through Fibers....53
A Company’s Story Crystallized: Dev Demand....54
Wrapping Up....55
Part II—Building Blocks....56
3. Typing Variables and Controlling the Flow....57
Converting Data Between Types....58
Getting Input....60
Putting It Together—Converting Currencies 1....62
Exception Handling for Faulty Input....64
Chaining Methods....66
Getting Input from Command-Line Arguments....67
Using String Methods....68
Using Symbols as Identifiers....71
Using Enums....72
Using Regular Expressions....72
Putting It Together—Converting Currencies 2....73
Beyond Hashes and Arrays: More Composite Types....74
Nilable Types....76
Controlling the Flow and Types....78
A Company’s Story Crystallized: Linkfeed....81
Wrapping Up....82
4. Organizing Code in Methods and Procs....83
Passing Arguments....84
Returning Values....86
Working with Yield, Procs, and Blocks....88
Overloading and Multiple Dispatch....94
Using a Shorter Syntax for Exception Handling....97
Using Recursive Methods....98
A Company’s Story Crystallized: Duo Design....100
Wrapping Up....102
5. Using Classes and Structs....103
Converting a Ruby Class to Crystal....103
Structuring a Class....110
Applying Inheritance....113
Controlling Visibility....116
Working with Structs....121
Viewing the Type Hierarchy....123
Some Nice Tricks....124
A Company’s Story Crystallized: LI-COR Biosciences....128
Wrapping Up....129
6. Working with Modules....130
Combining Files with Require....130
Using Modules as Namespaces....133
Letting Modules Extend Themselves....135
Mixing in Modules....136
How the Compiler Finds Methods....137
Applying Built-In Modules....138
A Company’s Story Crystallized: Diploid....140
Wrapping Up....142
7. Managing Projects....143
Creating a Shard....143
Formatting Code....146
Documenting a Project....146
Writing Tests with Spec....148
Using External Libraries....152
Benchmarking Your Code....156
Deploying a Crystal App....158
A Company’s Story Crystallized: Diploid—Part 2....158
Wrapping Up....160
Part III—Advanced Crystal....161
8. Advanced Features....162
DRY Your Code with Macros....162
Low-Level Programming and C Bindings....167
Creating Concurrent Code....170
Accessing Databases....178
A Company’s Story Crystallized: NeuraLegion....182
Wrapping Up....183
9. Using Web Frameworks and Shards....184
Build Web Applications with the Kemal and Amber Frameworks....184
A Brief Tour of the Shard Ecosystem....198
A Company’s Story Crystallized: Kemal in Production....200
Wrapping Up and Afterword....201
Part IV—Appendices....202
A1. Setting Up a Crystal Environment....203
Working with Crystal Online....203
Installing Crystal on Your Machine....204
Compiling Code....205
Using Editors and IDEs....207
Working with Crystal Playground....210
Using Crystal Documentation....213
Wrapping Up....214
A2. Porting Ruby Code to Crystal....215
Ruby Extensions in Crystal....217
A3. Your Turn Answers....218
Chapter 2: Crystal Foundations....218
Chapter 3: Typing Variables and Controlling the Flow....219
Chapter 4: Organizing Code in Methods and Procs....220
Chapter 5: Using Classes and Structs....220
Chapter 6: Working with Modules....221
Chapter 7: Managing Projects....221
Chapter 8: Advanced Features....221
Chapter 9: Web Frameworks and the Shard Ecosystem....222
Appendix 1: Setting Up a Crystal Environment....222
Index....223
– SYMBOLS –....223
– A –....223
– B –....224
– C –....224
– D –....226
– E –....226
– F –....227
– G –....227
– H –....227
– I –....228
– J –....228
– K –....228
– L –....228
– M –....229
– N –....230
– O –....230
– P –....230
– Q –....231
– R –....231
– S –....232
– T –....233
– U –....234
– V –....234
– W –....234
– Y –....234
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.
To develop in Crystal, you only need Crystal v 0.26 the latest version, a common text editor and a browser.