ndh.00008B@gmail.com
4/18/2006 7:24:00 AM
Since classes are constants, you can get them through the module class
method const_get(someStringOrSymbol). But, I think that it would be
simpler just to pass the class constant itself, unless you are talking
about passing a string through a URL or something. There might be a
better way to do this, but this is what my looking through the API got
me.
The problem with this method is that it if the classes are not
available during runtime--they are not loaded into memory, it will
cause an error you would have to deal with because the constant won't
exist. But, I guess you probably expect that.
Here is some example code:
class LandLord
@name
attr_accessor :name
end
class Apartment
@floor
attr_accessor :floor
end
def test(klassStr)
Module.const_get(klassStr).new
end
apt=test("Apartment")
apt.floor = 1
bob = test("LandLord")
bob.name = "Bob, the Land Lord"
puts bob.name
#Or using the class as a constant
klass = LandLord
joe = klass.new
joe.name = "Joe, The Main Man"
puts joe.name