Ruby Cheat Sheet
Ruby syntax, blocks, symbols, mixins, and enumerable methods for expressive, object-oriented scripting and web development.
2 PagesBeginnerMar 25, 2026
Basic Syntax
Variables, control flow, and iteration.
ruby
age = 30name = "Ada"pi = 3.14159is_fun = trueif age >= 18 puts "#{name} is an adult"end5.times do |i| puts "Count: #{i}"end
Blocks & Iterators
Enumerable methods with blocks.
ruby
nums = [5, 3, 1, 4, 2]nums.each { |n| puts n }evens = nums.select { |n| n.even? } # [4, 2]squared = nums.map { |n| n * n } # [25, 9, 1, 16, 4]sum = nums.reduce(0) { |acc, n| acc + n } # 15sorted = nums.sort # [1, 2, 3, 4, 5]
Core Enumerable Methods
Frequently used methods on collections.
- .each- iterates over a collection without returning a new one
- .map- transforms each element, returns a new array
- .select / .reject- filters elements by a block's truthiness
- .reduce (inject)- accumulates a single value across elements
- .nil? / .empty?- checks for nil or an empty collection/string
- .to_s / .to_i- converts an object to a String/Integer
- attr_accessor- generates getter and setter methods for instance variables
Classes & Mixins
Object-oriented Ruby with modules.
ruby
module Greetable def greet "Hello, #{name}" endendclass Person include Greetable attr_accessor :name, :age def initialize(name, age) @name = name @age = age endendp = Person.new("Ada", 30)puts p.greet # "Hello, Ada"
String Manipulation
Common string operations and interpolation.
ruby
name = "ruby"puts "Hello, #{name.capitalize}!" # Hello, Ruby!"hello world".split(" ") # ["hello", "world"]" trim ".strip # "trim""abcabc".gsub("a", "X") # "XbcXbc""hello".chars # ["h","e","l","l","o"]"a,b,c".split(",").join("-") # "a-b-c""Ruby".include?("ub") # true"%05d" % 42 # "00042""data.csv".end_with?(".csv") # true
Hashes
Working with key-value collections.
ruby
user = { name: "Ada", age: 36 }user[:name] # "Ada"user.fetch(:email, "n/a") # default if missinguser.each { |k, v| puts "#{k}: #{v}" }user.keys # [:name, :age]user.merge(city: "London") # adds a keyuser.select { |k, v| v.is_a?(Integer) } # {age: 36}user.transform_values(&:to_s) # stringify valuescounts = Hash.new(0)"aabbc".each_char { |c| counts[c] += 1 }
Conditionals & Symbols
Ruby control flow expressions and case matching.
ruby
status = :activeresult = case status when :active then "running" when :paused then "waiting" else "unknown" end# Modifier formputs "adult" if age >= 18puts "loop" while counter < 5# Ternary and safe navigationlabel = valid ? "ok" : "bad"email = user&.profile&.email # nil if any link is nilx = nilx ||= "default" # assign only if falsy
Naming Conventions
Ruby idioms signaled by naming.
- method?- predicate returning a boolean (empty?, nil?)
- method!- mutating or dangerous variant (sort! sorts in place)
- snake_case- methods and variables
- CamelCase- class and module names
- SCREAMING_SNAKE- constants
- @ivar- instance variable scoped to an object
- @@cvar- class variable shared across instances
- $global- global variable (rarely used)
Pro Tip
Use `Symbol` (:name) instead of `String` for hash keys and identifiers — symbols are immutable and reused, making comparisons faster than string equality checks.
Was this cheat sheet helpful?
Explore Topics
#Ruby#RubyCheatSheet#Programming#Beginner#BasicSyntax#BlocksIterators#CoreEnumerableMethods#ClassesMixins#OOP#Functions#WebDevelopment#CommandLine#CheatSheet#SkillVeris