module Protocol
class Packet
def self.packet_id
rand(10)
end
end
class A < Packet
end
class B < Packet
end
class NotAPacket
end
end
mapping = Protocol.constants.each_with_object({}) do |constant_name, result|
klass = Protocol.const_get(constant_name)
next unless klass.is_a?(Class)
next unless klass <= Protocol::Packet
result[klass.packet_id] = klass
end
p mapping
bW9kdWxlIFByb3RvY29sCiAgY2xhc3MgUGFja2V0CiAgICBkZWYgc2VsZi5wYWNrZXRfaWQKICAgICAgcmFuZCgxMCkKICAgIGVuZAogIGVuZAoKICBjbGFzcyBBIDwgUGFja2V0CiAgZW5kCgogIGNsYXNzIEIgPCBQYWNrZXQKICBlbmQKCiAgY2xhc3MgTm90QVBhY2tldAogIGVuZAplbmQKCm1hcHBpbmcgPSBQcm90b2NvbC5jb25zdGFudHMuZWFjaF93aXRoX29iamVjdCh7fSkgZG8gfGNvbnN0YW50X25hbWUsIHJlc3VsdHwKICBrbGFzcyA9IFByb3RvY29sLmNvbnN0X2dldChjb25zdGFudF9uYW1lKQogIG5leHQgdW5sZXNzIGtsYXNzLmlzX2E/KENsYXNzKQogIG5leHQgdW5sZXNzIGtsYXNzIDw9IFByb3RvY29sOjpQYWNrZXQKICByZXN1bHRba2xhc3MucGFja2V0X2lkXSA9IGtsYXNzCmVuZAoKcCBtYXBwaW5nCg==