Getting Started
5 snippetsYour first steps with Ruby basics
Hello World
puts "Hello, World!" Comments
# Single line comment
=begin
Multi-line
comment
=end User Input
print "Name: "
name = gets.chomp Type Conversion
"42".to_i # string to int
42.to_s # int to string
"3.14".to_f # to float Check Type
variable.class # String, Integer
variable.is_a?(String) # true/false Variables & Data Types
7 snippetsDeclaring and using different data types
String
name = "John" Integer
age = 25 Float
price = 19.99 Array
fruits = ["apple", "banana", "cherry"] Hash
person = { name: "John", age: 30 } Symbol
:status Range
(1..5) # 1 to 5 inclusive
(1...5) # 1 to 4 Operators
6 snippetsSymbols for calculations and comparisons
Arithmetic
5 + 3 # 8 (addition)
5 - 3 # 2 (subtraction)
5 * 3 # 15 (multiplication)
5 / 3 # 1 (integer division)
5.0 / 3 # 1.666... (float division)
5 % 3 # 2 (modulus)
5 ** 3 # 125 (exponent) Comparison
5 == 5 # true (equal)
5 != 3 # true (not equal)
5 > 3 # true (greater)
5 <=> 3 # 1 (spaceship: -1, 0, 1)
5.eql?(5) # true (same type) Logical
true && false # false (AND)
true || false # true (OR)
!true # false (NOT)
true and false # false (lower precedence)
true or false # true (lower precedence) Assignment
x = 5 # Assign
x += 3 # x = 8 (add)
x -= 2 # x = 6 (subtract)
x *= 2 # x = 12 (multiply)
x ||= 10 # x = 12 (assign if nil/false) Range
(1..5) # 1, 2, 3, 4, 5 (inclusive)
(1...5) # 1, 2, 3, 4 (exclusive)
('a'..'e').to_a # ["a", "b", "c", "d", "e"] Safe Navigation
user&.name # nil if user is nil
user&.profile&.email # safe chain Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
9 snippetsRepeating code blocks efficiently
Each
(1..5).each do |i|
puts i
end Each One-liner
[1, 2, 3].each { |n| puts n } Times
5.times { |i| puts i } # 0, 1, 2, 3, 4 Upto/Downto
1.upto(5) { |i| puts i }
5.downto(1) { |i| puts i } While
while count < 10
count += 1
end Until
until count >= 10
count += 1
end Loop
loop do
break if done
end Each with Index
arr.each_with_index do |item, i|
puts "#{i}: #{item}"
end Break/Next
(1..10).each do |i|
next if i == 5 # skip
break if i == 8 # exit
puts i
end Conditionals
5 snippetsMaking decisions in your code
If/Else
if x > 0
puts "positive"
elsif x < 0
puts "negative"
else
puts "zero"
end Unless
puts "active" unless inactive Ternary
result = condition ? "yes" : "no" Case
case day
when "Mon"
puts "Monday"
when "Tue", "Wed"
puts "Midweek"
else
puts "Other"
end One-line If
puts "big" if x > 100 Methods
5 snippetsMethod
def greet(name)
"Hello, #{name}!"
end Default Arg
def greet(name = "World")
"Hello, #{name}!"
end Splat Args
def sum(*nums)
nums.reduce(0, :+)
end Keyword Args
def greet(name:, age: 0)
"#{name} is #{age}"
end Block
def with_timing
start = Time.now
yield
Time.now - start
end Array Methods
6 snippetsMap
[1, 2, 3].map { |n| n * 2 } Select
[1, 2, 3, 4].select { |n| n.even? } Reject
[1, 2, 3, 4].reject { |n| n.even? } Reduce
[1, 2, 3].reduce(0) { |sum, n| sum + n } Find
users.find { |u| u.id == 1 } Sort
[3, 1, 2].sort
users.sort_by { |u| u.age } Classes & OOP
4 snippetsClass
class Dog
attr_accessor :name
def initialize(name)
@name = name
end
def bark
"#{@name} says woof!"
end
end Inheritance
class Puppy < Dog
def play
"#{@name} is playing!"
end
end Module
module Greetable
def greet
"Hello!"
end
end Include
class Person
include Greetable
end String Operations
6 snippetsInterpolation
"Hello, #{name}!" Concatenate
"Hello" + " " + "World" Split
"a,b,c".split(",") Join
["a", "b", "c"].join(",") Strip
" hello ".strip Replace
"hello".gsub("l", "x") Error Handling
3 snippetsBegin/Rescue
begin
risky_operation
rescue StandardError => e
puts e.message
end Ensure
begin
# code
rescue
# handle
ensure
cleanup
end Raise
raise ArgumentError, "Invalid input"