What Makes Hanami the Hidden Gem of Ruby Web Frameworks?

Exploring Hanami: A Poetic and Efficient Ruby Web Framework

What Makes Hanami the Hidden Gem of Ruby Web Frameworks?

When chatting about Ruby web frameworks, Ruby on Rails often steals the show. But honestly, there are loads of cool alternatives worth checking out. One that’s pretty interesting is Hanami. It’s like the modern, lightweight option that’s all about simplicity and speed. So, let’s jump into what makes Hanami special and how to get rolling with it.

Getting to Know Hanami

First off, let’s clear the air—Hanami used to be called Lotus. Luca Guidi is the genius behind it. The name “Hanami” is inspired by the Japanese tradition of flower viewing. Pretty poetic, right? Hanami was designed to be a modern Ruby framework. Its main jam is speed, built-in security, and keeping things lean and mean.

Why Hanami Stands Out

Alright, here’s the scoop:

Multiple Apps in One Project

Unlike Rails where you get one giant app, Hanami lets you have multiple apps within a single project. So, if you need a JSON-driven API and a regular web application side by side, it’s totally doable. This flexibility is a lifesaver for complex projects needing different app types.

Speed and Minimalism

Hanami is all about keeping it minimal. It skips the big structure Rails has, making it faster and more memory-efficient. Word on the street is that Hanami uses 60% less memory than other Ruby frameworks. So, if your app needs to be snappy, Hanami’s your go-to.

Features Over MVC

Rails is pretty strict with its MVC (Model-View-Controller) structure. Hanami, on the other hand, is more about the features. This means you’ve got more freedom to build your app the way you want, without sticking to a rigid structure.

Callable Objects and OOP

Hanami brings some extra oomph to object-oriented programming. With its mixin-injection system, you can easily build complex objects from simpler ones. It’s pretty slick.

Why Pick Hanami?

Productivity and Maintainability

Luca Guidi wanted to boost productivity and keep things simple. Hanami helps you build better software without the chaos of complex frameworks like Rails. This makes it super attractive for experienced developers who just want to get stuff done efficiently.

Security and Performance

Hanami doesn’t slack when it comes to security. It’s got built-in features to fend off common security threats, taking advantage of the latest browser technologies. Its lightweight components and optimized setup keep everything running fast. Whether it’s an API or rich web app, Hanami’s got your back.

Getting Started with Hanami

Setting Up

First things first, make sure your Ruby environment is all set. Then, install the Hanami gem using the command line:

gem install hanami

Once that’s done, you can whip up a new Hanami project:

hanami new my_app

This creates a basic Hanami project structure, and you’re ready to roll.

Building a Simple Hanami App

Let’s build something simple. After generating your project, head to your project directory and start the server:

cd my_app
hanami server

You can now access your app at http://localhost:2300. Hanami’s generators make it quick and easy to create standard code for requests, database migrations, and mailers. Say you want a new controller:

hanami generate controller my_controller

This command sets up the files you need for your controller, simplifying the whole process.

Real-Life Examples

Hanami is versatile. You can use it for building all sorts of apps. Here’s a couple of examples:

Fast API Apps

Hanami has a separate library for API applications, making it one of the quickest frameworks out there. Its lightweight setup means speedy APIs.

Rich Web Apps

Hanami supports assets, mailers, validators—you name it. These built-in features and generators help you build robust web applications fast and efficiently.

Who Should Use Hanami?

Hanami is basically a dream for experienced developers. It’s got a learning curve, so it’s not really for beginners. If you’ve been around the block and are looking for a simpler, more maintainable way to build apps, Hanami is up your alley. It encourages you to create your own “magic” rather than relying on the often confusing features of larger frameworks like Rails.

Wrapping It Up

Hanami is a breath of fresh air in Ruby web development. It’s all about simplicity, speed, and maintainability. While it might not be the best pick for newbies, it’s a fantastic tool for seasoned devs wanting to build top-notch applications quickly. With its lean architecture, built-in security, and flexible approach, Hanami is definitely worth a shot. Whether it’s a blazing-fast API or a feature-rich web app, Hanami has what you need to nail your next project. So, if you’re an experienced developer looking to try something new, give Hanami a whirl. It might just become your new favorite framework.