stephen O'D
4/8/2009 8:38:00 PM
Lets say I have a two classes, Foo and Bar.
In some code, I have a variable that can either contain the test 'Foo'
or the text 'Bar'. If it contains Foo, I would like to call Foo.new
(v1, v2), if it contains Bar, I would like to call Bar.new(v1, v2).
Obviously I could create an IF statement, such as
if var == 'Foo' then
Foo.new(...)
elsif var == 'Bar' then
Bar.new(...)
end if
Or if I want to get fancy, maybe a dispatch table:
dispatch = {
'Foo' => Foo,
'Bar' => Bar
}
obj = dispatch[var].new(...)
Is there another Ruby way to do this sort of thing?
Thanks,
Stephen.