Programming Crystal: Create High-Performance, Safe, Concurrent Apps

Programming Crystal: Create High-Performance, Safe, Concurrent Apps

Programming Crystal: Create High-Performance, Safe, Concurrent Apps

Автор: Ivo Balbaert , Simon St. Laurent
Дата выхода: 2019
Издательство: The Pragmatic Programmers
Количество страниц: 238
Размер файла: 3,0 МБ
Тип файла: PDF
Добавил: zedchi
 Проверить на вирусы  Дополнительные материалы 

Preface . . . . . . . . . . . . . . ix

Part I — Getting Started
1. Diving into Crystal . . . . . . . . . . . 3
A Programming Language for Humans and Computers 3
Slick As Ruby, But Way Faster 4
Almost As Fast As C 7
Speeding Up the Web 9
Talking to Databases 11
More Safety Through Types 12
No to the Billion-Dollar Mistake 13
Batteries Included 14
Putting Crystal to Good Use 15
A Company’s Story Crystallized: Red Panthers 16
Wrapping Up 18


2. Crystal Foundations . . . . . . . . . . . 19
Using Basic Variables and Types 19
Variable Operations 22
Structuring Data with Container Types 23
Controlling the Flow 28
Using Methods 33
Organizing Code in Classes and Modules 34
Executing Code Concurrently Through Fibers 38
A Company’s Story Crystallized: Dev Demand 39
Wrapping Up 40


Part II — Building Blocks
3. Typing Variables and Controlling the Flow . . . . . 43
Converting Data Between Types 44
Getting Input 46
Putting It Together—Converting Currencies 1 48
Exception Handling for Faulty Input 50
Chaining Methods 52
Getting Input from Command-Line Arguments 53
Using String Methods 54
Using Symbols as Identifiers 57
Using Enums 58
Using Regular Expressions 58
Putting It Together—Converting Currencies 2 59
Beyond Hashes and Arrays: More Composite Types 60
Nilable Types 62
Controlling the Flow and Types 64
A Company’s Story Crystallized: Linkfeed 67
Wrapping Up 68


4. Organizing Code in Methods and Procs . . . . . . 69
Passing Arguments 70
Returning Values 72
Working with Yield, Procs, and Blocks 74
Overloading and Multiple Dispatch 80
Using a Shorter Syntax for Exception Handling 83
Using Recursive Methods 84
A Company’s Story Crystallized: Duo Design 86
Wrapping Up 88


5. Using Classes and Structs . . . . . . . . . 89
Converting a Ruby Class to Crystal 89
Structuring a Class 96
Applying Inheritance 99
Controlling Visibility 102
Working with Structs 107
Viewing the Type Hierarchy 109
Some Nice Tricks 110
A Company’s Story Crystallized: LI-COR Biosciences 114
Wrapping Up 115

Contents • vi


6. Working with Modules . . . . . . . . . . 117
Combining Files with Require 117
Using Modules as Namespaces 120
Letting Modules Extend Themselves 122
Mixing in Modules 123
How the Compiler Finds Methods 124
Applying Built-In Modules 125
A Company’s Story Crystallized: Diploid 127
Wrapping Up 129


7. Managing Projects . . . . . . . . . . . 131
Creating a Shard 131
Formatting Code 134
Documenting a Project 134
Writing Tests with Spec 136
Using External Libraries 140
Benchmarking Your Code 144
Deploying a Crystal App 146
A Company’s Story Crystallized: Diploid—Part 2 146
Wrapping Up 148

Part III — Advanced Crystal
8. Advanced Features . . . . . . . . . . . 151
DRY Your Code with Macros 151
Low-Level Programming and C Bindings 156
Creating Concurrent Code 159
Accessing Databases 167
A Company’s Story Crystallized: NeuraLegion 171
Wrapping Up 172


9. Using Web Frameworks and Shards . . . . . . . 173
Build Web Applications with the Kemal
and Amber Frameworks 173
A Brief Tour of the Shard Ecosystem 187
A Company’s Story Crystallized: Kemal in Production 189
Wrapping Up and Afterword 190

Contents • vii


Part IV — Appendices
A1. Setting Up a Crystal Environment . . . . . . . 193
Working with Crystal Online 193
Installing Crystal on Your Machine 194
Compiling Code 195
Using Editors and IDEs 197
Working with Crystal Playground 200
Using Crystal Documentation 203
Wrapping Up 204


A2. Porting Ruby Code to Crystal . . . . . . . . 205
Ruby Extensions in Crystal 207


A3. Your Turn Answers . . . . . . . . . . . 209


Chapter 2: Crystal Foundations 209
Chapter 3: Typing Variables and Controlling the Flow 210
Chapter 4: Organizing Code in Methods and Procs 211
Chapter 5: Using Classes and Structs 211
Chapter 6: Working with Modules 212
Chapter 7: Managing Projects 212
Chapter 8: Advanced Features 212
Chapter 9: Web Frameworks and the Shard Ecosystem 213
Appendix 1: Setting Up a Crystal Environment 213
Index . . . . . . . . . . . . . . 215

 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.


Список отзывов:

Нет отзывов к книге.