fork download
  1. module Protocol
  2. class Packet
  3. def self.packet_id
  4. rand(10)
  5. end
  6. end
  7.  
  8. class A < Packet
  9. end
  10.  
  11. class B < Packet
  12. end
  13.  
  14. class NotAPacket
  15. end
  16. end
  17.  
  18. mapping = Protocol.constants.each_with_object({}) do |constant_name, result|
  19. klass = Protocol.const_get(constant_name)
  20. next unless klass.is_a?(Class)
  21. next unless klass <= Protocol::Packet
  22. result[klass.packet_id] = klass
  23. end
  24.  
  25. p mapping
  26.  
Success #stdin #stdout 0s 4716KB
stdin
Standard input is empty
stdout
{2=>Protocol::Packet, 9=>Protocol::A, 1=>Protocol::B}