Ruby

On this page 8

Ruby is a dynamic, interpreted, object-oriented programming language created by Yukihiro Matsumoto and first released in 1995, designed around the principle that code should be pleasant for humans to read and write. Among general-purpose languages in programming, Ruby stands out for treating every value as an object, for its expressive block syntax and for strong support for metaprogramming, which lets programs define and modify their own behavior at runtime.

Ruby gained global adoption through Ruby on Rails, the web framework released in 2004 that popularized convention over configuration and rapid application development. Today Ruby remains a common choice for web back ends, developer tooling, automation scripts and domain-specific languages, while its performance has improved substantially through successive interpreter releases.

How Ruby executes code

Ruby source code is executed by an interpreter that compiles it to bytecode for a virtual machine rather than to native machine code ahead of time. The reference implementation, known as CRuby or MRI, has used the YARV virtual machine since Ruby 1.9, and recent versions add YJIT, a just-in-time compiler that translates frequently executed bytecode into native instructions.

CRuby uses a Global VM Lock (GVL), which allows only one thread to execute Ruby code at a time within a process. Threads still help with input and output bound work such as network calls, because the lock is released while waiting, but CPU-heavy parallelism traditionally requires multiple processes. Ruby 3.0 introduced Ractors, an actor-style model that enables true parallel execution with isolated object spaces, although adoption remains limited.

The following table summarizes the main Ruby implementations and where each fits.

ImplementationRuntime basisTypical use
CRuby (MRI)YARV bytecode VM with optional YJITDefault for most applications
JRubyJava Virtual MachineJava integration, real thread parallelism
TruffleRubyGraalVM with partial evaluationHigh peak performance experiments
mrubyLightweight embeddable VMEmbedded devices and game scripting
Major Ruby implementations

Core language features of Ruby

Ruby is purely object-oriented: integers, strings, nil and even classes are objects that respond to methods, so an expression such as 5.times { puts "hi" } is ordinary Ruby. The language uses dynamic typing with duck typing, meaning an object is judged by the methods it responds to rather than by its declared class.

Blocks are anonymous chunks of code passed to methods with do...end or braces, and they underpin iteration, resource management and callbacks. Ruby replaces multiple inheritance with mixins: a class can include one or more modules to share behavior, as the Enumerable and Comparable modules demonstrate across the standard library.

The table below lists features that shape everyday Ruby code.

FeatureWhat it providesPractical effect
Blocks and iteratorsCode passed to methodsConcise loops and resource handling
Mixins via modulesShared behavior without multiple inheritanceReusable traits across classes
Open classesReopening existing classes at runtimeExtensions, but risk of conflicts
method_missingHandling of undefined method callsDynamic proxies and DSLs
SymbolsImmutable interned identifiersEfficient hash keys and names
Garbage collectionAutomatic memory managementNo manual allocation or freeing
Defining features and their effect

Metaprogramming tools such as define_method, instance_variable_get and send allow libraries to generate methods dynamically. This power explains the readable internal DSLs found in tools like RSpec and Rake, but it also makes static analysis and code navigation harder than in explicitly typed languages.

The Ruby ecosystem and tooling

Ruby libraries are distributed as gems through the RubyGems package manager, and Bundler resolves and locks dependency versions per project in a Gemfile.lock. Version managers such as rbenv, chruby and asdf let developers run several Ruby versions side by side.

Beyond Rails, the ecosystem includes Sinatra for lightweight web services, Sidekiq for background job processing, RSpec and Minitest for testing, and RuboCop for style enforcement. Optional static typing is available through RBS signature files, shipped with Ruby 3.0, and through Sorbet, a gradual type checker developed for large codebases.

Ruby is most often compared with other dynamic languages used for web development and scripting. Its closest relative in philosophy is Python, while its main web competitors include PHP and JavaScript on Node.js.

LanguageTypingDesign emphasisDominant domain
RubyDynamic, duck typingExpressiveness and developer happinessWeb applications, tooling
PythonDynamic, optional hintsOne obvious way to do thingsData science, scripting, web
PHPDynamic, optional declarationsPragmatic server-side webContent management, web
JavaScriptDynamic, prototype-basedUbiquity in browsersFront end and Node.js back ends
GoStatic, compiledSimplicity and concurrencyCloud services, infrastructure
Ruby and neighboring languages

The key contrast with Python is cultural: Ruby tolerates many ways to express the same idea, whereas Python favors a single idiomatic form. Compared with Go or Java, Ruby trades raw execution speed and compile-time safety for faster iteration and shorter code.

When to choose Ruby

Ruby is a strong choice when time to market and code clarity matter more than maximum throughput. The decision table below maps common situations to a recommendation.

SituationRecommendationReason
Database-backed web productRuby with RailsMature conventions and fast delivery
Build and automation scriptsRuby is suitableReadable syntax and rich standard library
CPU-intensive numerical workPrefer another languageLimited numeric ecosystem and GVL
Low-latency systems softwarePrefer Rust, Go or C++Garbage collection and interpreter overhead
Internal domain-specific languageRuby is well suitedBlocks and metaprogramming enable DSLs
Decision guide for adopting Ruby

Limitations and common pitfalls

The most cited weakness of Ruby is performance relative to compiled languages, although YJIT has narrowed the gap for typical web workloads. Memory consumption per process can be high, which matters because scaling often relies on multiple worker processes rather than threads.

  • Monkey patching core classes can create conflicts between gems and surprising behavior.
  • Heavy metaprogramming makes code difficult to trace and to refactor safely.
  • Dynamic typing defers many errors to runtime, so thorough automated testing is essential.
  • Rails conventions speed up delivery but can hide costly database queries such as the N+1 query problem.

The simple explanation

Ruby is a way of writing instructions for a computer that tries to read almost like plain English. Its creator wanted programmers to enjoy writing code, so Ruby lets you express an idea in few words.

Think of it as a well-organized kitchen where every tool is within reach. You can cook quickly and creatively, although a professional industrial kitchen might produce meals faster at huge scale. In short, Ruby favors speed of building over speed of running, which makes it popular for websites and online services.

How it is used in practice

A small team of four developers at a regional logistics startup needs a customer portal where clients book shipments, track parcels and download invoices. They choose Ruby on Rails because its generators, built-in database migrations and conventions let them model shipments, customers and invoices in days rather than weeks.

Within six weeks the team ships a working portal backed by PostgreSQL, uses Sidekiq background jobs to send tracking emails and runs an RSpec suite of about 900 tests on every commit. As traffic grows, they enable YJIT and add Puma worker processes, keeping median response times under 150 milliseconds without rewriting the application in another language.