-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.rb
More file actions
45 lines (38 loc) · 901 Bytes
/
Copy pathfactory.rb
File metadata and controls
45 lines (38 loc) · 901 Bytes
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
class Factory
def initialize(*args, &block)
Customer.class_eval do
define_method "initialize" do |*elems|
elems.each_with_index do |elem, index|
self.public_send("#{args[index]}=",elem)
end
end
define_method "[]" do |arg|
begin
if arg.class == Fixnum
self.public_send("#{args[arg]}")
else
self.public_send(arg)
end
rescue
raise "Invalid argument"
end
end
args.each do |arg|
attr_accessor arg.to_sym
end
class_eval(&block) if block_given?
end
end
end
class Customer
end
Сustomer = Factory.new(:name, :address, :zip) do
def greeting
puts "Hello, #{name}!"
end
end
Customer.new("Joe", "Maple st.", 12345).greeting
joe = Customer.new("Joe", "Maple st.", 12345)
puts joe[:name]
puts joe["address"]
puts joe[2]