Ruby Cheat Sheet

Ruby programming language essentials — syntax, blocks, iterators, gems, Rails basics, and idiomatic Ruby patterns for elegant code.

Last Updated: July 15, 2025

Ruby Basics

FeatureExample
Variablesname = "Ruby" (no type declaration)
String interpolation"Hello, #{name}"
Symbols:symbol — immutable, memory-efficient strings
Arraysarr = [1, 2, 3]; arr << 4
Hasheshash = {key: "value", foo: "bar"}

Blocks & Iterators

PatternExample
Each[1,2,3].each { |n| puts n }
Map[1,2,3].map { |n| n * 2 } → [2,4,6]
Select[1,2,3,4].select { |n| n.even? } → [2,4]
Do...end3.times do |i| puts i end

Classes & Modules

FeatureExample
Classclass User; attr_accessor :name; def initialize(name); @name = name; end; end
Inheritanceclass Admin < User; end
Module (mixin)module Loggable; def log(msg); puts msg; end; end

Popular Gems

GemPurpose
RailsFull-stack web framework
RSpecTesting framework
DeviseAuthentication for Rails
SidekiqBackground job processing
Pro Tip: Ruby's principle of least surprise: code should behave the way you expect. Methods ending in ? return booleans (empty?), methods ending in ! modify the receiver (sort!). This convention makes Ruby code read like English.
← Back to Programming Languages | Browse all categories | View all cheat sheets