320 x 50 Mobile Ad
Cheat Sheets

Ruby Cheat Sheet

Transform, format, and optimize your data instantly with this free client-side tool.

Basics

name = "Alice"

Variable assignment

puts "Hello, #{name}"

Print with string interpolation

print "Hello"

Print without trailing newline

p var

Print inspecting the object (calls .inspect)

x.class

Get the class/type of an object

x.to_i; x.to_s; x.to_f

Convert to Integer, String, Float

Control Structures

if x > 5 ... elsif x == 5 ... else ... end

If / Elsif / Else statement

puts 'Yes' if x == 10

Inline if statement

unless x > 5 ... end

Execute if condition is false

case x when 1 ... else ... end

Switch/Case statement

5.times { |i| puts i }

Times loop

while x > 0 x -= 1 end

While loop

Strings

s = 'hello'

String definition

s.upcase; s.downcase

Convert case

s.capitalize

Capitalize first letter

s.length; s.size

Get string length

s.include?('ell')

Check if includes substring

s.split(',')

Split string into array

s.gsub('old', 'new')

Global search and replace

s.strip

Remove leading and trailing whitespace

Arrays

arr = [1, 2, 3]

Array definition

arr << 4; arr.push(4)

Append element to end

arr.pop

Remove and return last element

arr.unshift(0)

Prepend element to beginning

arr.shift

Remove and return first element

arr.map { |x| x * 2 }

Create new array with transformed elements

arr.select { |x| x > 2 }

Filter array elements (like filter in JS)

arr.reduce(:+)

Sum elements in array

arr.join('-')

Join elements into a string

Hashes

h = { 'a' => 1, :b => 2, c: 3 }

Hash definition (String, Symbol syntax)

h[:c]

Access value by key

h.keys; h.values

Get all keys or values

h.key?(:c)

Check if key exists

h.delete(:c)

Remove key/value pair

h.each { |k, v| puts k }

Iterate over hash

Methods

def my_method(a, b = 2) a + b end

Method definition (last evaluated expression is returned)

def my_method(*args) ... end

Method accepting variable number of arguments

def my_method(**kwargs) ... end

Method accepting keyword arguments

yield if block_given?

Execute passed block if one exists

Classes

class MyClass ... end

Class definition

def initialize(name) @name = name end

Constructor method

attr_accessor :name

Create getter and setter methods for @name

attr_reader :age

Create getter method for @age

def self.class_method ... end

Class method definition

class Dog < Animal

Class inheritance

super

Call parent method with same arguments

Modules

module MyModule ... end

Module definition

include MyModule

Add module methods as instance methods

extend MyModule

Add module methods as class methods

Error Handling

begin ... rescue => e puts e.message ensure ... end

Error handling block

raise ArgumentError, 'msg'

Raise an exception manually

File I/O

content = File.read('file.txt')

Read entire file into string

File.write('file.txt', 'data')

Write string to file

File.open('file.txt', 'a') { |f| f.puts 'hi' }

Open file, append securely with block

File.exists?('file.txt')

Check if file exists

Advanced

Advanced Command Snippet

Explore advanced configurations for Ruby

System Optimization

Performance tuning best practices for Ruby

Security Audit

Run security checks and validation for Ruby

320 x 50 Mobile Ad

Frequently Asked Questions

Got questions? We've got answers.

The Ruby Cheatsheet is a quick-reference guide that provides developers with the most essential and frequently used Ruby commands, syntax, and snippets in one centralized place.
This reference is built for both beginners who are just learning Ruby and need a quick syntax lookup, as well as seasoned professionals who need to jog their memory on complex commands.
You can use the real-time search bar at the top of the cheatsheet. Simply type a keyword (like &#39;delete&#39; or &#39;file&#39;) and the list will instantly filter to show only matching Ruby commands.
Yes! Every command block features a one-click copy button. Just hover over the command and click the copy icon to instantly send the snippet to your clipboard.
The cheatsheet is divided into logical categories such as Basics, Network, Operations, and specific Ruby features. You can click on the category filters at the top to isolate specific groups of commands.
Absolutely. This Ruby reference guide is 100% free, requiring no sign-ups or subscriptions, and is always available when you need it.
While these are standard Ruby operations, you should always understand what a command does before running it, especially if it involves system operations or destructive actions.
Yes, we have recently expanded this cheatsheet to include advanced snippets, best practices, and edge-case syntax that go beyond basic introductory commands.
ADVERTISEMENT
Boost Your Business Online