Want to see the full-length video right now for free?
Crystal is a programming language with syntax unabashedly inspired by Ruby. It’s incredibly fast, statically typed, and an absolute pleasure to write.
Crystal helps you catch one of the most prevalent errors in programming, nil
errors.
class User
# This tells Crystal to allow a String or nil for the age
def initialize(@name : String?)
end
end
user = User.new(name: nil)
user.name.downcase
# Would result in an error at compile time
Error in hello_world.cr:8: undefined method 'downcase' for Nil (compile-time type is (String | Nil))
Lucky is a web framework written in Crystal that helps you catch bugs early, forget about most performance issues, and spend more time on code instead of debugging and writing tests.
Here’s an example of how a Lucky action helps you write reliable code:
class Announcements::Show < BaseAction
get "/announcements/:id" do
p id # Prints the :id param
render_text "List of announcements"
end
end
Lucky will inspect the path at compile time and create a method for every named parameter. This is what lets you call id
in the action. You are guaranteed that that parameter exists.
Lucky actions also generate a path helper for you:
Announcements::Show.route(id: "123") # Generates "/announcements/123"
The route
method ensure that all parameters are present. It’s also a lot easier to remember than path helpers in most frameworks because you call the action directly instead of some helper methods like announcement_path
.
Instead of using strings or symbols, Lucky generates Crystal methods from your model so that when you query the database, you’re guaranteed that those fields exist. No more worrying about renaming columns and forgetting to change a query, Lucky will catch it for you.
# Define a model
class Announcement < BaseModel
table :announcements do
field title : String
end
end
# Query it
Announcement::BaseQuery.new.title("This is the title").first
You also get type specific query methods. For example, you can use lower
with string types
Announcement::BaseQuery.new.title.lower.is(("this is the title")
You can write better applications thanks for Crystal’s lightning fast speed, helpful compiler, and beautiful syntax . Combine Crystal and Lucky and you can write beautiful, reliable, and fast applications that your customers are going to love.