Ruby Cheatsheet - Ruby Syntax & Methods Reference
This reference is for developers picking up Ruby (including Rails) or recalling its idioms. It starts with variables, symbols, and common built-in methods, then arrays/hashes, control flow (including postfix if and unless), methods and blocks/Proc/Lambda, classes & modules with attr_accessor, include/extend, and inheritance, and ends with begin-rescue-ensure exception handling. Unlike many languages, almost everything in Ruby is an object and methods return the value of their last expression, and blocks elegantly wrap iteration. After reading you should be able to write idiomatic, concise Ruby: iterate with blocks instead of hand loops, use symbols as hash keys, and organize reusable behavior into modules.
Basics 7
name = "Ruby"name = "Hello, #{name}":symbol# single-line comment=begin
comment block
=endniltrue / falseArray & Hash 9
arr = [1, 2, 3]arr << 4arr[0]arr.first / arr.lasthash = { a: 1, b: 2 }hash[:a]hash[:c] = 3hash.key?(:a)hash.keys / hash.valuesControl Flow 10
if x > 0\n puts "positive"\nendputs "positive" if x > 0unless x > 0\n puts "not positive"\nendcase x\nwhen 1 then "one"\nelse "other"\nend3.times { puts "hi" }1.upto(5) { |n| puts n }5.downto(1) { |n| puts n }loop { break if done }while x > 0\n x -= 1\nenduntil x == 0\n x -= 1\nendMethods 9
def greet(name)\n "Hello, #{name}"\nenddef greet(name = "World")def sum(*args)def method(a:, b:)return valuedef method\n yield if block_given?\nendmy_proc = Proc.new { |x| x*2 }my_lambda = ->(x) { x*2 }proc.call(5)Class & Module 10
class MyClass\nendattr_accessor :nameattr_reader :nameattr_writer :nameclass Child < Parentmodule MyModule\nendinclude MyModuleextend MyModuleself.class_methodclass << self\nendException 7
begin\n risky_call\nrescue\n "error"\nendrescue SpecificError => eensure\n cleanup\nendraise "error message"raise ArgumentError, "msg"retrycatch(:done) { throw(:done) }Built-in Methods 10
arr.map { |x| x*2 }arr.select { |x| x.even? }arr.reject { |x| x.even? }arr.reduce(0) { |s, x| s + x }hash.merge({ c: 3 })"hello".upcase"hello".split("")"hello".gsub(/l/, "L")arr.flattenarr.uniqBlocks & Iterators 7
[1, 2].each { |n| puts n }[1, 2].each do |n|\n puts n\nend(1..5).map { |n| n**2 }[1, 2, 3].select(&:even?)arr.partition { |x| x.even? }arr.chunk { |x| x > 0 }arr.group_by { |x| x % 3 }Tips
- Everything in Ruby is an object, including nil and numbers.
- each is the most common iterator; map transforms, select filters.
- Symbol :symbol is immutable and unique, good as a hash key.
- attr_accessor auto-generates getter and setter.
- Blocks are Ruby's most powerful feature; do...end and {} are equivalent.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us