Skip to content

Latest commit

 

History

History
498 lines (438 loc) · 7.67 KB

File metadata and controls

498 lines (438 loc) · 7.67 KB

Ruby

Hello World

puts "Hello World!"

Variables

age = 30
name = "John"

User input

print "Enter something: "
user_input = gets.chomp

If-else

if age >= 18
  puts "You are an adult."
else
  puts "You are a minor."
end

For loop

for i in 0..4
  puts i
end

While loop

count = 0
while count < 5
  puts count
  count += 1
end

Arrays

numbers = [1, 2, 3, 4, 5]

Array access

first_number = numbers[0]

Functions

def add(a, b)
  a + b
end

Object oriented programming

class Person
  attr_accessor :name, :age
  def greet
    puts "Hello, my name is #{@name}."
  end
end

Contructors and Object Initialization

class Person
  attr_accessor :name, :age
  def initialize(name, age)
    @name = name
    @age = age
  end
end

Inheritance

class Student < Person
  attr_accessor :school
end

List (Arrays)

fruits = ["apple", "banana", "cherry", "date", "fig"]

List operations (Add, Remove)

fruits.push("grape")
fruits.delete("cherry")

List iteration

fruits.each do |fruit|
  puts fruit
end

Hashes (dictionaries)

scores = {"Alice" => 95, "Bob" => 88, "Charlie" => 72}

Hash access and iteration

scores.each do |name, score|
  puts "#{name}: #{score}"
end

String Manipulation

text = "Hello, World!"
substring = text[0..4]

String concatenation

greeting = "Hello"
name = "Alice"
message = "#{greeting}, #{name}!"

DateTime

current_time = Time.now

Exception Handling (begin-rescue)

begin
  result = 10 / 0
rescue => e
  puts "Error: #{e.message}"
end

File reading

file = File.open("example.txt", "r")
content = file.read
file.close

###File writing

file = File.open("output.txt", "w")
file.puts "This is some text."
file.close

Working with Enums

module DaysOfWeek
  MONDAY = 0
  TUESDAY = 1
  WEDNESDAY = 2
  THURSDAY = 3
  FRIDAY = 4
  SATURDAY = 5
  SUNDAY = 6
end

Regular Expressions

text = "My email is example@example.com."
email_regex = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/
is_email = email_regex.match?(text)

Working with sets

require 'set'
unique_numbers = Set.new([1, 2, 3, 4, 5])

Set operations (intersection)

set1 = Set.new([1, 2, 3])
set2 = Set.new([2, 3, 4])
intersection = set1 & set2

command-line arguments

if ARGV.length > 0
  arg1 = ARGV[0]
  # Process command-line arguments
end

Random number generation

random_number = rand(1..10)

Working with symbols

status = :success

Enumerable methods (maps)

numbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.map { |n| n * n }

Enumerable methods (select)

numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |n| n.even? }

Enumerable methods (reduce)

numbers = [1, 2, 3, 4, 5]
sum = numbers.reduce(0) { |total, n| total + n }

Blocks and Yield

def custom_each(arr)
  for item in arr
    yield(item)
  end
end
custom_each([1, 2, 3]) { |x| puts x }

Procs and Lambdas

add = Proc.new { |a, b| a + b }
result = add.call(3, 4)

Modules and Mixins

module Greetable
  def greet
    puts "Hello!"
  end
end
class Person
  include Greetable
end

Custom exceptions

class CustomException < StandardError
  def initialize(message)
    super(message)
  end
end
begin
  raise CustomException.new("Custom exception occurred.")
rescue CustomException => e
  puts "Error: #{e.message}"
end

JSON serialization and deserialization

require 'json'
data = { "name" => "Alice", "age" => 30 }
json_string = data.to_json
parsed_data = JSON.parse(json_string)

Working with Timezones

require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
time_in_ny = tz.utc_to_local(Time.now.utc)

Working with Threads

t1 = Thread.new { puts "Thread 1" }
t2 = Thread.new { puts "Thread 2" }
t1.join
t2.join

Functional Programming (Map, Reduce, Filter)

numbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.map { |n| n * n }
total = numbers.reduce(0) { |sum, n| sum + n }
even_numbers = numbers.select(&:even?)

Modules and Namespace Resolution

module MyModule
  def self.my_method
    puts "MyModule.my_method"
  end
end
MyModule.my_method

Method missing and dynamic methods

class DynamicMethods
  def method_missing(method_name, *args)
    puts "Calling method: #{method_name}"
  end
end
obj = DynamicMethods.new
obj.some_dynamic_method

Singleton classes and methods

class MyClass
  def self.my_class_method
    puts "This is a class method."
  end
end
obj = MyClass.new
def obj.my_singleton_method
  puts "This is a singleton method."
end

Metaprogramming with define_method

class MyClass
  define_method :dynamic_method do |arg|
    puts "Dynamic method called with argument: #{arg}"
  end
end
obj = MyClass.new
obj.dynamic_method("Hello, Metaprogramming!")

Custom enumerators

class MyEnumerator
  include Enumerable
  def each
    yield 1
    yield 2
    yield 3
  end
end
enum = MyEnumerator.new
enum.each { |x| puts x }

Memoization with Hashes

def fibonacci(n, memo = {})
  return n if n <= 1
  memo[n] ||= fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
end

Closures with Lambdas

def closure_example
  x = 10
  lambda { |y| puts x + y }
end
closure = closure_example
closure.call(5)

Functional programming (currying)

add = lambda { |x| lambda { |y| x + y } }
add_five = add.call(5)
result = add_five.call(3)

Method chaining

class Calculator
  attr_accessor :value
  def initialize(value)
    @value = value
  end
  def add(x)
    @value += x
    self
  end
  def subtract(x)
    @value -= x
    self
  end
end
result = Calculator.new(10).add(5).subtract(3).value

Custom exceptions and error handling

class CustomException < StandardError
  def initialize(message)
    super(message)
  end
end
begin
  raise CustomException, "Custom exception occurred."
rescue CustomException => e
  puts "Error: #{e.message}"
end

Asynchronous Programming with Fibers (Fiber, Fiber.yield)

fiber = Fiber.new do
  puts "Fiber started"
  Fiber.yield
  puts "Fiber resumed"
end
puts "Main started"
fiber.resume
puts "Main resumed"

Multithreading with Threads and Mutexes

mutex = Mutex.new
counter = 0
threads = []
10.times do
  threads << Thread.new do
    mutex.synchronize { counter += 1 }
  end
end
threads.each(&:join)
puts "Counter: #{counter}"

Working with symbols

numbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.map(&:**2)

File I/O with Blocks (File.open)

File.open("example.txt", "r") do |file|
  content = file.read
end

Enumerable methods (reduce with symbols)

numbers = [1, 2, 3, 4, 5]
total = numbers.reduce(:+)

Custom Operators (Overloading)

class Complex
  attr_accessor :real, :imag
  def initialize(real, imag)
    @real = real
    @imag = imag
  end
  def +(other)
    Complex.new(@real + other.real, @imag + other.imag)
  end
end

Working with Sets (Set.intersection)

require 'set'
set1 = Set.new([1, 2, 3])
set2 = Set.new([2, 3, 4])
intersection = set1.intersection(set2)

Working with Dates and Times (Time.zone)

require 'active_support/time'
Time.zone = 'Eastern Time (US & Canada)'
current_time = Time.zone.now

Using tap for Method Chaining and Debugging

array = [1, 2, 3]
result = array.tap { |a| a << 4 }.map { |x| x * 2 }