require 'date'
PI = 3.14159
$global_var = "I am global"
module MathHelper
def self.square(x)
x * x
end
end
class Animal
attr_accessor :name
def initialize(name)
@name = name
end
def speak
raise NotImplementedError, "Subclasses must implement speak"
end
end
class Dog < Animal
def speak
"Woof!"
end
def self.info
puts "I am a dog class"
end
end
integer = 42
float = 3.14
string_dq = "Double quoted with interpolation: #{integer}"
string_sq = 'Single quoted string'
symbol = :my_symbol
array = [1, 2, "three", :four]
hash = { :name => "Alice", "age" => 30, city: "New York" }
range = 1..10
regex = /^[a-z0-9]+$/i
if integer > 10
puts "Greater than 10"
elsif integer == 10
puts "Equal to 10"
elsif integer < 0
puts "Negative"
else
puts "Less than 10"
end
unless integer < 0
puts "Not negative"
end
case integer
when 1..10
puts "Small"
when 42
puts "The Answer"
else
puts "Other"
end
5.times do |i|
puts "Iteration #{i}"
end
array.each { |element| puts element }
while integer > 40
integer -= 1
end
def greet(name, greeting = "Hello", *args, &block)
puts "#{greeting}, #{name}"
yield if block_given?
end
greet("Bob") { puts "Block executed" }
begin
result = 10 / 0
rescue ZeroDivisionError => e
puts "Error: #{e.message}"
ensure
puts "Cleanup"
end
my_proc = Proc.new { |x| puts x }
my_lambda = ->(x) { puts x * 2 }
my_proc.call(5)
my_lambda.call(5)
define_method(:dynamic_method) do
puts "Dynamic!"
end
__END__
Data after the script