mercredi 4 octobre 2017

Dynamically create class

I have the following code:

module City
  class Bus < Base
  end

  class BusOne < Bus; end
  class BusTwo < Bus; end
  class BusSixty < Bus; end
  ....
end

My goal is to create this classes dynamically:

  class BusOne < Bus; end
  class BusTwo < Bus; end
  class BusSixty < Bus; end
  ...

That's why I tried:

module City
  class Bus < Base
    DIVISON = [:one, :two, :sixty]
  end

  ....
  Bus::DIVISONS.each do |division|
    class "Bus#{division.capitalize}".constantize < Bus; end
  end
end

But I get this error:

unexpected '<', expecting &. or :: or '[' or '.' (SyntaxError)

What do I wrong? Thanks

Aucun commentaire:

Enregistrer un commentaire