On Wed, Oct 24, 2012 at 2:52 PM, The Sun <[email protected]> wrote:
> Thanks to Arlen andXavier,
>
> I now got it working, using the non eval approach.
>
> name = "DynamicName1"
> class = Class.new(ParentClass)
Won't work because "class" is one of the few keywords of Ruby:
$ ruby19 -ce 'class = Class.new(String)'
-e:1: syntax error, unexpected '='
class = Class.new(String)
^
> Object.const_set name, class
> class.new
>
> Great to be able to do this!
Please don't. It's no use to assign the name to a constant because if
you do not know names beforehand (i.e. when writing the program) as
seems to be the case here you need some form of lookup anyway. You
could use eval again, but a more simpler and robust approach (because
it avoids name clashes) is this:
dyn_classes = {}
name = "DynamicName1"
dyn_classes[name] = Class.new(ParentClass)
Btw, these classes are fairly useless because the only distinguishing
factor is the name. You'd also have to generate custom methods for
each of those. What is the purpose of these generated classes?
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en