Ruby Cheatsheet

1. Variables and Data Types

age = 30
is_student = true
name = 'John'
pi = 3.14159

# Array
elements = [1, 2, 3]

# Hash
person = { name: 'John', age: 30 }

2. Control Flow (Conditionals and Loops)

if age > 18
  puts 'Adult'
else
  puts 'Minor'
end

# Loops
5.times { |i| puts i }

while age < 40
do
  puts age
  age += 1
end

3. Methods and Blocks

def greet(name)
  "Hello, \#{name}!"
end
puts greet('John')

# Block
3.times { |i| puts i }

# Yield in a method
def custom_times(n)
  i = 0
  while i < n
    yield(i)
    i += 1
  end
end

custom_times(3) { |i| puts "Block iteration: \#{i}" }

4. Object-Oriented Programming (OOP)

class Animal
  def initialize(name)
    @name = name
  end
  def speak
    puts "\#{@name} makes a sound."
  end
end

class Dog < Animal
  def bark
    puts "\#{@name} barks."
  end
end

rex = Dog.new('Rex')
rex.speak
rex.bark

5. Modules and Mixins

module Walkable
  def walk
    puts 'I am walking'
  end
end

class Person
  include Walkable
end

p = Person.new
p.walk

6. Error Handling

begin
  result = 10 / 0
rescue ZeroDivisionError => e
  puts "Error: \#{e.message}"
else
  puts 'No error'
ensure
  puts 'This will always run'
end

7. Strings and Interpolation

name = 'John'
greeting = "Hello, \#{name}!"
puts greeting

# String methods
puts name.upcase
puts name.downcase
puts name.capitalize
puts name.reverse

8. Arrays and Hashes

# Arrays
numbers = [1, 2, 3, 4]

numbers.each { |num| puts num }

# Hashes
person = { name: 'John', age: 30 }
puts person[:name]

person.each { |key, value| puts "\#{key}: \#{value}" }

9. Enumerable Module and Iterators

# Array methods
numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |num| num.even? }
puts even_numbers

# Hash methods
person = { name: 'John', age: 30 }
person.each { |key, value| puts "\#{key}: \#{value}" }

10. File Handling

File.open('example.txt', 'w') { |file| file.write('Hello, Ruby!') }

File.open('example.txt', 'r') do |file|
  file.each_line { |line| puts line }
end

11. Symbols

name = :john
age = :age

puts name
puts age

# Symbols as hash keys
person = { name: 'John', age: 30 }
puts person[:name]

12. Regular Expressions

pattern = /\d+/
result = 'The number is 123'.scan(pattern)
puts result

# Using =~ operator
puts '123abc' =~ /\d+/

13. Procs and Lambdas

# Proc
greet_proc = Proc.new { puts 'Hello from Proc' }
greet_proc.call

# Lambda
greet_lambda = -> { puts 'Hello from Lambda' }
greet_lambda.call

14. Ranges

range = 1..5
range.each { |num| puts num }

# Exclusive range
exclusive_range = 1...5
exclusive_range.each { |num| puts num }

15. Metaprogramming

class MyClass
  def method_missing(method_name, *args, &block)
    puts "You called: \#{method_name}"
  end
end

obj = MyClass.new
obj.undefined_method

16. Constants and Global Variables

PI = 3.14159
puts PI

$global_var = 'I am global'
puts $global_var

17. Singleton Methods

str = 'hello'

def str.shout
  puts upcase
end

str.shout

18. Reflection and Introspection

puts self.class
puts self.class.ancestors

obj = 'hello'
puts obj.methods