Skip to content
Akshay's Blog
  • Archive
  • About Me
Sign in Subscribe
Hash with Indifferent Access

How to Access a Ruby Hash with both String and Symbol Keys

Sometimes, you receive a Hash key as a method parameter or via user input, and you want to make the Hash understand that key as-is, without worrying if the key is a string or a symbol. ActiveSupport's `HashWithIndifferentAccess` class lets you accomplish this in a convenient way.

Rails Internals 2 min read

Featured

A rack-compliant web application in Ruby

Let's Build a Web Application in Ruby without Rails

Ruby no Rails 10 min read
Books to learn programming with Ruby and Rails

A List of Books to Learn Programming with Ruby and Rails

8 min read
Crossroad in the woods

Understanding the Rails Router: Why, What, and How

21 min read
Hotwire: HTML Over the Wire

A Brief Introduction to Hotwire

Hotwire 8 min read
Debugging Rails Codebase

How to Debug and Step-Through Rails Codebase

Do you want to read the Rails source code for a deeper understanding of the framework, but feel intimidated by the sheer size of the codebase, or don't know where to start? Start with a specific feature, insert a breakpoint, and step through the method line-by-line. This article shows how.

Rails Internals 3 min read
Working with Cookies in Rails

The Complete Guide to Working With Cookies in Rails

This post covers almost everything you need to know about HTTP cookies in the context of Rails. We'll explore what a cookie is, why we need it, how to set & get a cookie, how to restrict cookies to a particular domain/path, prevent JavaScript access, how to sign & encrypt cookies, and much more.

14 min read
Middleware Pipeline

Rails Middleware: (Almost) Everything You Need to Know

In this post, we'll learn almost everything about Rails middleware: what it is, why we need it, how it works, and why it's so important. We'll also learn how to create and test custom middleware in a Rails app. Finally, we'll extract logging functionality in its own middleware in a simple web app.

Ruby no Rails 11 min read
Hotwire

Let's Read the Turbo Source: What Happens When You Click a Link?

Reading the source code is one of the best ways to learn how a feature, framework, or a program works. In this post, we'll explore the source code of Hotwire's Turbo library to understand exactly what happens when you click a link. I hope you'll appreciate Turbo much more after this deep dive.

11 min read
Simplified map function in Ruby

Let's Implement the Map Method from First Principles

Those new to functional programming often wonder when to use higher-order functions (or blocks/procs/lambdas in Ruby). I did, too. In this post, we'll implement the map method from scratch, in turn gaining an intuitive understanding of higher-order functions and when to use them.

5 min read
Active Record Database Migrations Cheatsheet

Rails Database Migrations Cheatsheet

The database schema evolves continuously. Rails migrations is a powerful and flexible way to update the schema without dropping and re-creating the database. However, all this power and flexibility can be overwhelming. Here's a handy cheatsheet for most common migration operations.

11 min read
Ruby's defined? Keyword

How to Check if a Variable is Defined in Ruby with defined? Keyword

Ruby's defined? keyword is an elegant, compact and fun way to check if a variable is defined or not and also to cache expensive operations. However, many gotchas await the new programmer. Now that I've had a few opportunities to work with it, here’s my shot at sparing you some of the confusion.

3 min read
Stimulus Controller

Practical Stimulus: Building a Counter Component

In this article, we will build a counter component using the Stimulus JavaScript library. This simple example will demonstrate a bunch of useful features of Stimulus such as managing state, handling events, and targeting DOM elements.

Practical Stimulus 4 min read
Useful Features of a Ruby Hash

Ruby's Hash is a Swiss-Army Knife

A Hash is a built-in data structure in Ruby that maps values to keys and has a constant-time O(1) lookup. This article shows the capabilities of this simple, but equally powerful tool. We’ll start with the basics but also cover some obscure but equally useful features of hash.

Ruby Handbook 10 min read
Ruby Standard Library: The Abbrev Module

Abbrev: A Hidden Ruby Gem

The Abbrev module in the Ruby standard library helps you find out all the possible and unique abbreviations for one or more strings. In this post, we'll learn how it works along with a practical example. We'll also take a peek behind the scenes to see how it's implemented.

4 min read
Zeitwerk without Rails

Using Zeitwerk Outside Rails

Did you know you can use Zeitwerk outside Rails? This article shows how to use the Zeitwerk gem to autoload classes and modules in a Ruby app that's not using Rails. We'll also learn how Rails configures Zeitwerk internally and introduce the concept of models in our no-rails Ruby web application.

Ruby no Rails 5 min read
Content Security Policy (CSP) API in Rails

How to Implement Content Security Policy in Rails

This article shows how to implement content security policy in your Rails applications to protect against cross-site scripting (XSS) vulnerability. We'll also learn how you can report CSP violations without enforcing the policy and make exceptions for inline scripts with nonce attributes.

8 min read

Page 1 of 11

Load More Something went wrong with loading more posts
  • Sign up

© 2023 - Akshay's Blog