File size: 3,202 Bytes
93d826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env ruby

global_var = "Hello, World!"

# Module for mixing in common functionality
module Loggable
  def log(message)
    puts "[#{Time.now}] #{message}"
  end
end

# Module with class methods
module Utils
  class << self
    def generate_id
      SecureRandom.uuid
    end
  end
end

# Abstract base class
class BaseProcessor
  include Loggable

  # Class instance variable
  @processors = []

  class << self
    attr_reader :processors

    def register(processor)
      @processors << processor
    end
  end

  # Instance variables with attr accessors
  attr_reader :id, :created_at
  attr_accessor :status

  def initialize
    @id = Utils.generate_id
    @created_at = Time.now
    @status = :pending
    self.class.register(self)
  end

  # Abstract method
  def process
    raise NotImplementedError, "#{self.class} must implement process"
  end
end

# Custom exception class
class ProcessingError < StandardError
  attr_reader :item

  def initialize(message, item)
    @item = item
    super(message)
  end
end

# Struct definition
User = Struct.new(:name, :email, keyword_init: true) do
  def valid?
    name && email && email.include?('@')
  end
end

# Enum-like module using freeze
module Status
  PENDING = 'pending'.freeze
  ACTIVE = 'active'.freeze
  COMPLETED = 'completed'.freeze
  FAILED = 'failed'.freeze

  ALL = [PENDING, ACTIVE, COMPLETED, FAILED].freeze
end

# Class using inheritance and mixins
class DataProcessor < BaseProcessor
  # Constants
  MAX_RETRIES = 3
  DEFAULT_TIMEOUT = 5

  # Class variable
  @@instance_count = 0

  def self.instance_count
    @@instance_count
  end

  def initialize(options = {})
    super()
    @options = options
    @items = []
    @@instance_count += 1
  end

  # Method with keyword arguments and default value
  def add_item(item:, priority: :normal)
    validate_item(item)
    @items << [item, priority]
  end

  # Private methods
  private

  def validate_item(item)
    raise ArgumentError, "Invalid item" unless item.respond_to?(:valid?)
    raise ProcessingError.new("Invalid item", item) unless item.valid?
  end

  # Method using block
  def with_retry
    retries = 0
    begin
      yield
    rescue StandardError => e
      retries += 1
      retry if retries < MAX_RETRIES
      raise
    end
  end

  # Method using lambda
  def process_items
    sorter = ->(a, b) { a[1] <=> b[1] }
    @items.sort(&sorter).each do |item, _priority|
      process_item(item)
    end
  end

  protected

  def process_item(item)
    log("Processing item: #{item}")
    # Processing logic here
  end
end

# Singleton class
require 'singleton'
class Configuration
  include Singleton

  def initialize
    @settings = {}
  end

  def [](key)
    @settings[key]
  end

  def []=(key, value)
    @settings[key] = value
  end
end

# Example usage
if __FILE__ == $PROGRAM_NAME
  config = Configuration.instance
  config[:timeout] = 30

  processor = DataProcessor.new(timeout: config[:timeout])
  user = User.new(name: "John Doe", email: "john@example.com")

  begin
    processor.add_item(item: user, priority: :high)
    processor.process
  rescue ProcessingError => e
    puts "Failed to process #{e.item}: #{e.message}"
  end
end