Ruby Cheat Sheet

Free Ruby cheat sheet: the most-used Ruby syntax and methods at a glance — searchable and beginner-friendly.

Basics

ConceptSyntaxExample
Variable
No declaration keyword; types are dynamic.
name = valuecount = 0 name = "Ada"
Print
puts adds a newline; print does not; p inspects.
puts x / print xputs "Hello"
String interpolation
Only works inside double-quoted strings.
"#{expr}"puts "Hi #{name}, age #{age + 1}"
Comment
Use =begin / =end for multi-line comments.
# comment# this is a comment
Conditional
Blocks end with end; elsif (no second e).
if / elsif / else / endif age >= 18 puts "adult" end

Strings & Symbols

ConceptSyntaxExample
String methods
Bang versions like upcase! mutate in place.
str.upcase / .length / .strip"hello".upcase # "HELLO"
Concatenate
<< appends in place and is more efficient.
str + str / str << str"foo" + "bar" # "foobar"
Split & join
split parses into an array; join collapses back.
str.split / arr.join"a,b,c".split(",") # ["a","b","c"]
Symbol
Immutable, interned identifiers — efficient hash keys.
:name:status { name: "Ada" }
Format / convert
Explicit type conversions between common types.
to_s / to_i / to_sym42.to_s # "42" "42".to_i # 42

Arrays & Hashes

ConceptSyntaxExample
Array
<< pushes onto the end.
[a, b, c]nums = [1, 2, 3] nums << 4
Access & slice
Negative indices count from the end: arr[-1].
arr[i] / arr[a..b]nums[0] # 1 nums[1..2] # [2, 3]
Hash
Symbol keys can use shorthand: { name: "Ada" }.
{ key => value }ages = { "Ada" => 36, "Bob" => 28 }
Access hash value
fetch raises if the key is missing; [] returns nil.
hash[key]ages["Ada"] # 36
Common methods
select filters; reduce(:+) sums; reject is the inverse of select.
.map / .select / .reducenums.map { |n| n * 2 }

Blocks & Iterators

ConceptSyntaxExample
each
The fundamental iterator; runs the block per element.
coll.each { |x| ... }nums.each { |n| puts n }
Block with do/end
do/end for multi-line blocks; { } for one-liners.
coll.each do |x| ... endnums.each do |n| puts n * 2 end
map (transform)
Returns a new array of transformed values.
coll.map { |x| expr }doubled = nums.map { |n| n * 2 }
times loop
Repeats the block n times, passing the index.
n.times { ... }3.times { |i| puts i }
Range iteration
.. includes the end; ... excludes it.
(a..b).each { }(1..5).each { |i| puts i }

Classes & Modules

ConceptSyntaxExample
Define a class
initialize is the constructor; @name is an instance variable.
class Name ... endclass Person def initialize(name) @name = name end end
Method
The last expression is returned implicitly.
def name(args) ... enddef greet "Hi #{@name}" end
Attribute accessors
Generates getter and setter methods. attr_reader for read-only.
attr_accessor :nameattr_accessor :name, :age
Inheritance
< means "inherits from"; use super to call the parent.
class B < Aclass Dog < Animal end
Module / mixin
Modules share behavior across classes via include.
module M ... end + includemodule Greetable def hello; "hi"; end end class User; include Greetable; end

Common Patterns

ConceptSyntaxExample
Ternary / inline if
puts "yes" if ok is a readable postfix form.
cond ? a : bstatus = age >= 18 ? "adult" : "minor"
Safe navigation
Returns nil instead of erroring if obj is nil.
obj&.methoduser&.name
Default with ||=
Assigns only if x is nil or false.
x ||= valueconfig ||= {}
Nil check
unless is if not; nil and false are the only falsy values.
x.nil? / unlessputs "missing" if name.nil?
Symbol-to-proc
Shorthand for { |x| x.upcase }.
&:methodnames.map(&:upcase)