Ruby Cheat Sheet
Free Ruby cheat sheet: the most-used Ruby syntax and methods at a glance — searchable and beginner-friendly.
Basics
| Concept | Syntax | Example |
|---|---|---|
| Variable No declaration keyword; types are dynamic. | name = value | count = 0
name = "Ada" |
| Print puts adds a newline; print does not; p inspects. | puts x / print x | puts "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 / end | if age >= 18
puts "adult"
end |
Strings & Symbols
| Concept | Syntax | Example |
|---|---|---|
| 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_sym | 42.to_s # "42"
"42".to_i # 42 |
Arrays & Hashes
| Concept | Syntax | Example |
|---|---|---|
| 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 / .reduce | nums.map { |n| n * 2 } |
Blocks & Iterators
| Concept | Syntax | Example |
|---|---|---|
| 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| ... end | nums.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
| Concept | Syntax | Example |
|---|---|---|
| Define a class initialize is the constructor; @name is an instance variable. | class Name ... end | class Person
def initialize(name)
@name = name
end
end |
| Method The last expression is returned implicitly. | def name(args) ... end | def greet
"Hi #{@name}"
end |
| Attribute accessors Generates getter and setter methods. attr_reader for read-only. | attr_accessor :name | attr_accessor :name, :age |
| Inheritance < means "inherits from"; use super to call the parent. | class B < A | class Dog < Animal
end |
| Module / mixin Modules share behavior across classes via include. | module M ... end + include | module Greetable
def hello; "hi"; end
end
class User; include Greetable; end |
Common Patterns
| Concept | Syntax | Example |
|---|---|---|
| Ternary / inline if puts "yes" if ok is a readable postfix form. | cond ? a : b | status = age >= 18 ? "adult" : "minor" |
| Safe navigation Returns nil instead of erroring if obj is nil. | obj&.method | user&.name |
| Default with ||= Assigns only if x is nil or false. | x ||= value | config ||= {} |
| Nil check unless is if not; nil and false are the only falsy values. | x.nil? / unless | puts "missing" if name.nil? |
| Symbol-to-proc Shorthand for { |x| x.upcase }. | &:method | names.map(&:upcase) |