Ruby Cheat Sheet
Ruby syntax, blocks, symbols, mixins, and enumerable methods for expressive, object-oriented scripting and web development.
2 PagesBeginnerMar 25, 2026
Basic Syntax
Variables, control flow, and iteration.
ruby
age = 30name = "Ada"pi = 3.14159is_fun = trueif age >= 18 puts "#{name} is an adult"end5.times do |i| puts "Count: #{i}"end
Blocks & Iterators
Enumerable methods with blocks.
ruby
nums = [5, 3, 1, 4, 2]nums.each { |n| puts n }evens = nums.select { |n| n.even? } # [4, 2]squared = nums.map { |n| n * n } # [25, 9, 1, 16, 4]sum = nums.reduce(0) { |acc, n| acc + n } # 15sorted = nums.sort # [1, 2, 3, 4, 5]
Core Enumerable Methods
Frequently used methods on collections.
- .each- iterates over a collection without returning a new one
- .map- transforms each element, returns a new array
- .select / .reject- filters elements by a block's truthiness
- .reduce (inject)- accumulates a single value across elements
- .nil? / .empty?- checks for nil or an empty collection/string
- .to_s / .to_i- converts an object to a String/Integer
- attr_accessor- generates getter and setter methods for instance variables
Classes & Mixins
Object-oriented Ruby with modules.
ruby
module Greetable def greet "Hello, #{name}" endendclass Person include Greetable attr_accessor :name, :age def initialize(name, age) @name = name @age = age endendp = Person.new("Ada", 30)puts p.greet # "Hello, Ada"
Pro Tip
Use `Symbol` (:name) instead of `String` for hash keys and identifiers — symbols are immutable and reused, making comparisons faster than string equality checks.
Was this cheat sheet helpful?
Explore Topics
#Ruby#RubyCheatSheet#Programming#Beginner#BasicSyntax#BlocksIterators#CoreEnumerableMethods#ClassesMixins#OOP#Functions#WebDevelopment#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance