summaryrefslogtreecommitdiff
path: root/samples/test.rb
blob: 3fbfb9fde418e2c27eab12113ed540ace14b4877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# A simple Ruby class demonstration

class Person
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age
  end

  def introduce
    puts "Hi, I'm #{@name} and I am #{@age} years old."
  end

  def can_vote?
    @age >= 18
  end
end

# Module definition
module Greeter
  def self.say_hello(name)
    puts "Hello, #{name}!"
  end
end

# Main execution
if __FILE__ == $0
  alice = Person.new("Alice", 25)
  alice.introduce

  if alice.can_vote?
    puts "#{alice.name} can vote."
  else
    puts "#{alice.name} cannot vote."
  end

  Greeter.say_hello("Bob")

  # Array and block
  numbers = [1, 2, 3, 4, 5]
  squared = numbers.map { |n| n * n }
  puts "Squared numbers: #{squared.inspect}"

  # Hash
  config = {
    :env => "production",
    :retries => 3,
    :timeout => 500
  }
  
  puts "Environment: #{config[:env]}"
end