Generate four positions by TYPE: a, b, c, and d.
class ShainFactory
def create(type, alphabet)
shain = nil
if type == 'a'
shain = a.new(alphabet)
elsif type == 'b'
shain = b.new(alphabet)
elsif type == 'c'
shain = c.new(alphabet)
elsif type == 'd'
shain = d.new(alphabet)
end
shain
end
end
Since it is too long, use eval to make it clear. eval executes the specified string as Ruby program code and returns the result. If the content of type is a
, a.new
is returned.
class ShainFactory
def create(type, alphabet)
eval "#{type}.new(alphabet)"
end
end
The code is now clear.