担当、主任、部長、取締役の4つの役職をtype別に生成する。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class ShainFactory
def create(type, kihonkyu)
shain = nil
if type == 'Tanto'
shain = Tanto.new(kihonkyu)
elsif type == 'Shunin'
shain = Shunin.new(kihonkyu)
elsif type == 'Bucho'
shain = Bucho.new(kihonkyu)
elsif type == 'Torishimariyaku'
shain = Torishimariyaku.new(kihonkyu)
end
shain
end
end
|
長すぎるのでevalを使ってスッキリさせる。evalは指定した文字列をRubyのプログラムコードとして実行し、その結果を返す。 typeの中身がTantoであればTanto.newを返す。
1
2
3
4
5
|
class ShainFactory
def create(type, kihonkyu)
eval "#{type}.new(kihonkyu)"
end
end
|
スッキリしました。
See Also