puts "Hello World!"age = 30
name = "John"print "Enter something: "
user_input = gets.chompif age >= 18
puts "You are an adult."
else
puts "You are a minor."
endfor i in 0..4
puts i
endcount = 0
while count < 5
puts count
count += 1
endnumbers = [1, 2, 3, 4, 5]first_number = numbers[0]def add(a, b)
a + b
endclass Person
attr_accessor :name, :age
def greet
puts "Hello, my name is #{@name}."
end
endclass Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
endclass Student < Person
attr_accessor :school
endfruits = ["apple", "banana", "cherry", "date", "fig"]fruits.push("grape")
fruits.delete("cherry")fruits.each do |fruit|
puts fruit
endscores = {"Alice" => 95, "Bob" => 88, "Charlie" => 72}scores.each do |name, score|
puts "#{name}: #{score}"
endtext = "Hello, World!"
substring = text[0..4]greeting = "Hello"
name = "Alice"
message = "#{greeting}, #{name}!"current_time = Time.nowbegin
result = 10 / 0
rescue => e
puts "Error: #{e.message}"
endfile = 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.closemodule DaysOfWeek
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
SATURDAY = 5
SUNDAY = 6
endtext = "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)require 'set'
unique_numbers = Set.new([1, 2, 3, 4, 5])set1 = Set.new([1, 2, 3])
set2 = Set.new([2, 3, 4])
intersection = set1 & set2if ARGV.length > 0
arg1 = ARGV[0]
# Process command-line arguments
endrandom_number = rand(1..10)status = :successnumbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.map { |n| n * n }numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |n| n.even? }numbers = [1, 2, 3, 4, 5]
sum = numbers.reduce(0) { |total, n| total + n }def custom_each(arr)
for item in arr
yield(item)
end
end
custom_each([1, 2, 3]) { |x| puts x }add = Proc.new { |a, b| a + b }
result = add.call(3, 4)module Greetable
def greet
puts "Hello!"
end
end
class Person
include Greetable
endclass CustomException < StandardError
def initialize(message)
super(message)
end
end
begin
raise CustomException.new("Custom exception occurred.")
rescue CustomException => e
puts "Error: #{e.message}"
endrequire 'json'
data = { "name" => "Alice", "age" => 30 }
json_string = data.to_json
parsed_data = JSON.parse(json_string)require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
time_in_ny = tz.utc_to_local(Time.now.utc)t1 = Thread.new { puts "Thread 1" }
t2 = Thread.new { puts "Thread 2" }
t1.join
t2.joinnumbers = [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?)module MyModule
def self.my_method
puts "MyModule.my_method"
end
end
MyModule.my_methodclass DynamicMethods
def method_missing(method_name, *args)
puts "Calling method: #{method_name}"
end
end
obj = DynamicMethods.new
obj.some_dynamic_methodclass 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."
endclass MyClass
define_method :dynamic_method do |arg|
puts "Dynamic method called with argument: #{arg}"
end
end
obj = MyClass.new
obj.dynamic_method("Hello, Metaprogramming!")class MyEnumerator
include Enumerable
def each
yield 1
yield 2
yield 3
end
end
enum = MyEnumerator.new
enum.each { |x| puts x }def fibonacci(n, memo = {})
return n if n <= 1
memo[n] ||= fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
enddef closure_example
x = 10
lambda { |y| puts x + y }
end
closure = closure_example
closure.call(5)add = lambda { |x| lambda { |y| x + y } }
add_five = add.call(5)
result = add_five.call(3)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).valueclass CustomException < StandardError
def initialize(message)
super(message)
end
end
begin
raise CustomException, "Custom exception occurred."
rescue CustomException => e
puts "Error: #{e.message}"
endfiber = Fiber.new do
puts "Fiber started"
Fiber.yield
puts "Fiber resumed"
end
puts "Main started"
fiber.resume
puts "Main resumed"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}"numbers = [1, 2, 3, 4, 5]
squared_numbers = numbers.map(&:**2)File.open("example.txt", "r") do |file|
content = file.read
endnumbers = [1, 2, 3, 4, 5]
total = numbers.reduce(:+)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
endrequire 'set'
set1 = Set.new([1, 2, 3])
set2 = Set.new([2, 3, 4])
intersection = set1.intersection(set2)require 'active_support/time'
Time.zone = 'Eastern Time (US & Canada)'
current_time = Time.zone.nowarray = [1, 2, 3]
result = array.tap { |a| a << 4 }.map { |x| x * 2 }