Ruby Cheat Sheet
Last updated
Hello World & running code
Ruby needs no boilerplate - a single line runs.
| Operation | Syntax |
|---|---|
| Print with newline | puts "Hello, World!" |
| Print without newline | print "Hello" |
| Inspect a value | p [1, 2, 3] |
| String interpolation | puts "Hi #{name}" |
| Comment | # this is a comment |
| Multi-line comment | =begin ... =end |
| Run a file | ruby app.rb |
| Interactive shell | irb |
Variables & types
Ruby is dynamically typed; everything is an object.
| Operation | Syntax |
|---|---|
| Local variable | age = 30 |
| Constant | PI = 3.14 |
| Instance variable | @name = "Ada" |
| Global variable | $count = 0 |
| Symbol | :status |
| Nil / boolean | nil, true, false |
| Check type | 42.class returns Integer |
| Convert type | "42".to_i, 42.to_s, "3.5".to_f |
Strings
Strings are mutable objects with a rich method set.
| Operation | Syntax |
|---|---|
| Length | "hello".length |
| Upper / lower case | s.upcase, s.downcase |
| Trim whitespace | s.strip |
| Replace | s.gsub("a", "b") |
| Split into array | "a,b,c".split(",") |
| Includes substring | s.include?("ell") |
| Slice characters | s[0..2] |
| Concatenate | "foo" + "bar", s << "!" |
| Repeat | "ab" * 3 |
| Format | format("%05d", 42) |
Arrays
Ordered, integer-indexed collections.
| Operation | Syntax |
|---|---|
| Create | nums = [1, 2, 3] |
| Access by index | nums[0], nums[-1] |
| Add to end | nums.push(4), nums << 4 |
| Remove from end | nums.pop |
| Length | nums.length |
| Map | nums.map { |n| n * 2 } |
| Filter | nums.select { |n| n.even? } |
| Reduce | nums.reduce(0) { |sum, n| sum + n } |
| Sort | nums.sort |
| Join to string | nums.join(", ") |
Hashes
Key-value collections, often keyed by symbols.
| Operation | Syntax |
|---|---|
| Create | user = { name: "Ada", age: 30 } |
| Access value | user[:name] |
| Set value | user[:email] = "a@x.com" |
| Check key | user.key?(:name) |
| Delete key | user.delete(:age) |
| Keys / values | user.keys, user.values |
| Iterate | user.each { |k, v| puts "#{k}: #{v}" } |
| Default value | Hash.new(0) |
| Merge | h1.merge(h2) |
Control flow
Conditionals and loops; note Ruby's trailing modifiers.
| Operation | Syntax |
|---|---|
| If / elsif / else | if x > 0 ... elsif x < 0 ... else ... end |
| Inline if (modifier) | puts "hi" if ready |
| Unless | unless done ... end |
| Ternary | x > 0 ? "pos" : "neg" |
| Case / when | case n; when 1 then ...; else ...; end |
| While loop | while i < 10 ... end |
| Until loop | until done ... end |
| Times | 5.times { |i| puts i } |
| Range iteration | (1..5).each { |i| puts i } |
| Break / next | break, next |
Methods
The last expression is returned implicitly.
| Operation | Syntax |
|---|---|
| Define a method | def add(a, b) ... end |
| Implicit return | def square(x) x * x end |
| Default argument | def greet(name = "World") ... end |
| Keyword arguments | def box(w:, h:) ... end |
| Splat (variadic) | def sum(*nums) ... end |
| Method ending in ? | def valid? ... end |
| Method ending in ! | def normalize! ... end |
| Call with no parens | greet "Ada" |
Blocks, procs & lambdas
Blocks are chunks of code passed to methods; procs and lambdas store them.
| Operation | Syntax |
|---|---|
| Block (braces) | [1, 2].each { |n| puts n } |
| Block (do/end) | [1, 2].each do |n| puts n end |
| Yield to a block | def run; yield; end |
| Block given check | block_given? |
| Capture block as param | def run(&blk); blk.call; end |
| Create a proc | square = proc { |x| x * x } |
| Create a lambda | square = ->(x) { x * x } |
| Call a proc / lambda | square.call(3), square.(3) |
Classes & modules
Classes hold state and behavior; modules mix in shared methods.
| Operation | Syntax |
|---|---|
| Define a class | class Point ... end |
| Constructor | def initialize(x, y) @x = x; @y = y; end |
| Instance method | def dist ... end |
| Accessor shortcuts | attr_accessor :x, :y |
| Create instance | p = Point.new(1, 2) |
| Inherit | class Circle < Shape ... end |
| Class method | def self.origin ... end |
| Define a module | module Drawable ... end |
| Mix in a module | include Drawable |
| Call super | super(args) |
The Ruby syntax you reach for most, on one page. This Ruby cheat sheet is a quick reference for the core language - variables and types, strings, arrays and hashes, control flow, methods, and the blocks, procs, and lambdas that make Ruby so expressive.
Everything here is plain Ruby and runs on any standard interpreter. Copy what you need, or try every snippet live in the Ruby playground - no install required.
Ruby cheat sheet FAQ
Is this Ruby cheat sheet free?
Yes. This Ruby cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up a string method, hash operation, or block syntax.
What is the difference between a block, a proc, and a lambda?
{ ... } or do ... end - it is not an object on its own. A proc wraps a block in a callable object you can store and pass around. A lambda is a special proc that checks its argument count strictly and treats return as returning from the lambda itself, whereas a plain proc returns from the enclosing method.What are symbols in Ruby?
:name is an immutable, reusable identifier. Because the same symbol always points to the same object in memory, symbols are faster and more memory-efficient than strings for things like hash keys and method names - which is why you often see hashes written as { name: "Ada" }.Can I practice Ruby online?
Yes. Open the Ruby playground to run any snippet from this cheat sheet in your browser - no Ruby install needed. When you want structure, Coddy's free interactive Ruby course takes you from variables and strings to blocks and classes step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the most common topics (variables, strings, control flow) down to advanced ones (blocks, procs, classes), so you can use the top sections on day one and grow into the rest.