Ruby Cheat Sheet
Complete reference for Ruby language methods, classes, and system variables.
Kernel & Core
Kernel (Global Methods)
Pre-defined Variables
Global Variables
- $! Last Exception Message
- $@ Backtrace of last exception
- $_ Last input line (gets)
- $. Current line number
- $0 Script name
- $* Command line arguments (ARGV)
- $$ Process ID (PID)
- $? Status of last child process
- $: Load path
- $stdin Standard Input
- $stdout Standard Output
- $stderr Standard Error
- ENV Environment Variables (Hash)
- ARGV Command line args array
- RUBY_VERSION Ruby Version String
- RUBY_PLATFORM OS Platform
Regex Variables
- $& Last match string
- $` String left of match
- $' String right of match
- $1, $2.. Capture groups
- $~ Last match info object
- $+ Last bracket matched
Strings & Arrays
String
- new / try_convert
- + / * / % / <<
- == / === / =~
- [] / []= / slice
- length / size / bytesize
- empty? / include?
- upcase / downcase / swapcase
- capitalize / chomp / chop
- strip / lstrip / rstrip
- split / chars / lines
- sub / gsub / replace
- index / rindex / match
- scan / partition
- to_i / to_f / to_s / to_sym
- start_with? / end_with?
- center / ljust / rjust
- reverse / each_char
- encode / force_encoding
Array
- new / []
- push / pop / shift / unshift
- first / last / take / drop
- each / each_index
- map / collect
- select / reject / keep_if
- delete / delete_at
- compact / flatten / uniq
- sort / sort_by / reverse
- join / to_s
- include? / empty?
- length / size / count
- index / rindex
- sample / shuffle
- zip / transpose / product
- & (Intersection) / | (Union)
- + (Concat) / - (Diff)
Numbers & Math
File, IO & System
Time & Misc
Enumerator & Proc