Integrating .ruby-version into your Rails Gemfile ruby

Integrating .ruby-version into your Rails Gemfile

Stop declaring your Ruby version twice: with a recent Bundler your Gemfile can read .ruby-version directly using ruby file: ".ruby-version". The one-line setup, the required Bundler upgrade, and how it keeps rbenv, RVM, or asdf and Rails in sync.

Jul 22, 2024

What is Turbo Frames in Rails? ruby

What is Turbo Frames in Rails?

Turbo Frames let Rails reload just a fragment of the page instead of the whole thing, with no custom JavaScript. What Turbo Frames are within Hotwire, a practical example of wrapping views in frames, and the responsiveness gains you get almost for free.

Jun 17, 2024

How to run Thruster and Kamal in Rails app

How to run Thruster and Kamal in Rails app

Thruster with Kamal in Rails: what 37signals' no-config proxy adds (HTTP/2, Let's Encrypt HTTPS, asset caching, X-Sendfile with compression), why it is still useful behind Traefik or Cloudflare, and how to integrate it into your app.

Implementing read and write databases in Rails

Implementing read and write databases in Rails

Rails read and write databases in practice: configure a primary and replica in database.yml, enable automatic connection switching, update ApplicationRecord, and handle the replication challenges that come with splitting the load.

How to create rake task and pass arguments

How to create rake task and pass arguments

Creating rake tasks and passing arguments, four ways: the default bracket syntax, OptParse, raw ARGV, and Linux-style flags, plus namespaces and prerequisites for organizing tasks in a Rails app, each with a working example.

Adding Custom Inflections in Rails with Zeitwerk

Adding Custom Inflections in Rails with Zeitwerk

Custom inflections in Rails with Zeitwerk: use ActiveSupport::Inflector in config/initializers/inflections.rb to register acronyms like API, CSV, or SSL, so autoloading maps file names to the capitalized class names you want.

How to work with JSONB column in pure Ruby on Rails

How to work with JSONB column in pure Ruby on Rails

JSONB columns in Ruby on Rails without extra gems: use store_accessor to read and write keys like normal attributes, set sensible defaults, and validate required keys with a small module. Practical code you can drop into a PostgreSQL-backed Rails app.

What is ETag and how to use it in Rails for caching?

What is ETag and how to use it in Rails for caching?

ETags let browsers skip downloading content that has not changed. What entity tags are in HTTP, then the Rails caching toolkit: stale?, fresh_when, expires_in, expires_now, public requests, and http_cache_forever, each shown with a concrete controller example.

What is ACID?

What is ACID?

ACID stands for Atomicity, Consistency, Isolation, and Durability: the four guarantees behind reliable database transactions. Each property explained with a concrete Rails example, like wrapping a money transfer in a transaction so it fully succeeds or rolls back.

What is instance and class variable in Ruby?

What is instance and class variable in Ruby?

Instance variables versus class variables in Ruby: what @ and @@ actually mean, why an @ variable set at the class level stays empty when read from an instance, and how @@ values are shared by every instance. Short code samples make each behavior easy to verify.

Powerful switch statement with pattern matching in Ruby

Powerful switch statement with pattern matching in Ruby

Pattern matching in Ruby turns case statements into a precise switch over data shapes: match and destructure hashes from an API response, combine several patterns in one branch, capture values across patterns, and validate configuration. Ruby 2.7+ syntax examples.

How to set up XML in Grape API

How to set up XML in Grape API

XML support in Grape API takes two steps: register the additional content_type so endpoints accept and return application/xml, then change the root element name of the XML output. Complete Ruby examples built around a products endpoint you can adapt to your API.

Multiple ways to execute a shell command via Ruby

Multiple ways to execute a shell command via Ruby

Running shell commands from Ruby: system, backticks, exec, the IO class, spawn, and Open3.popen3 compared with pros and cons, from quick one-liners to capturing stdout, stderr, and exit status, so you can pick the right method for scripts and production code.