struct Struct new id name configs struct new Foo struct new Bar config

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
struct = Struct.new :id, :name
configs = [struct.new(1,"Foo"), struct.new(2, "Bar")]
configs.each do |config|
class_name = "Clazz#{config.name}"
source_id = config.id
x = Class.new do
end
x.class_eval <<-RUBY
def id
#{source_id}
end
RUBY
Kernel.const_set class_name, x
end
puts ClazzFoo.new.id
puts ClazzBar.new.id