gpt4 book ai didi

ruby - 从未知模块访问值,委托(delegate)给未知方法

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:08 25 4
gpt4 key购买 nike

我正在写一个 network protocol decoder对于许多不同的协议(protocol)。此应用程序是一个命令行应用程序,不使用 Rails。

示例代码来说明我的挑战(Live Demo):

#!/usr/bin/env ruby

# define the base protocol handler

module Base
MSG_HASH = {0x30=>:One,0x31=>:Two,0x32=>:Three}

class Decoder
def decode(wire)
# get the msg type
msg_type = MSG_HASH[wire]
# delegate to a handler for that msg type
send("decode_#{msg_type}_message".to_sym)
end

def decode_One_message
"This was a One message"
end

def decode_Two_message
"This was a Two message"
end
end
end

# define a handler for a specialized protocol

module Special
MSG_HASH = Base::MSG_HASH.merge({0x4a=>:Foo, 0x73=>:Bar})

class Decoder < Base::Decoder
def decode_Foo_msg
"Specialized Foo message"
end
def decode_Bar_message
"Specialized Bar message"
end
end
end

# define a handler for another specialized protocol
module AnotherSpecial
MSG_HASH = Base::MSG_HASH.merge({0x4a=>:Zippity, 0x73=>:DooDaa})

class Decoder < Base::Decoder
def decode_Zippity_message
"Zippity"
end
def decode_DooDaa_message
"DooDaa"
end
end
end

# decode the first specialized protocol
puts "Decoding the first protocol"
[0x30, 0x31, 0x4A, 0x73].each do |wire|
decoder = Special::Decoder.new
puts "(#{wire}) decoded to '#{decoder.decode(wire)}'"
end

# decode the second specialized protocol
puts "Decoding the second protocol"
[0x30, 0x31, 0x4A, 0x73].each do |wire|
decoder = AnotherSpecial::Decoder.new
puts "(#{wire}) decoded to '#{decoder.decode(wire)}'"
end

不正确的输出是:

Decoding the first protocol
(48) decoded to 'This was a One message'
(49) decoded to 'This was a Two message'
./hacks:13:in `decode': undefined method `decode__message' for #<Special::Decoder:0x00000001ccac58> (NoMethodError)
from ./hacks:59:in `block in <main>'
from ./hacks:57:in `each'
from ./hacks:57:in `<main>'

但我希望它是:

Decoding the first protocol
(48) decoded to 'This was a One message'
(49) decoded to 'This was a Two message'
(74) decoded to 'Specialized Foo message'
(115) decoded to 'Specialized Bar message'
Decoding the second protocol
(48) decoded to 'This was a One message'
(49) decoded to 'This was a Two message'
(74) decoded to 'Zippity'
(115) decoded to 'DooDaa'

我认为这里的基本问题是 Base::Decoder::decode 不知道添加到 Special::MSG_HASH 的额外值。怎么可能,对吧?

我的基本约束是 Base 模块无法知道有关专用协议(protocol)或处理这些专用协议(protocol)的模块或类中的任何内容的任何。例如,更改 Base::decoder 以提及 SpecialAnotherSpecial 模块,或其中的任何内容,对我来说都不起作用。

我该如何解决这个问题?

不要害怕将我的代码撕成碎片。我完全不确定我的解决方案是否完全错乱


如果您想知道我的限制,请让我解释一下。我正在为网络协议(protocol)编写解码器。其中一些协议(protocol)共享公共(public)子协议(protocol),并且在这些子协议(protocol)中有一些字段(如消息类型),这些字段可以具有由子协议(protocol)或专用协议(protocol)定义的值。

一个例子是消息类型。消息类型字段存在于共享 XMT 子协议(protocol)的所有消息中。一些由消息类型标识的消息是在 XMT 协议(protocol)中定义的,而另一些是由专门的协议(protocol)定义的。那些专门的消息类型对于专门的协议(protocol)是唯一的,并且可能存在重叠。例如,在 ABC 专用协议(protocol)中,消息类型 0x77 可能表示 Quote,而在 XYZ 协议(protocol)中它可能表示 Replay。因此专用消息类型只能由专用协议(protocol)的处理程序定义。但是消息类型 0x30 是由 XMT 协议(protocol)定义的,并且始终表示 Heartbeat

最佳答案

使其工作的一种方法是将专用消息哈希移动到特殊类中,然后使用 self.class::MSG_HASH 访问它。

这是完整的代码;它给出了你想要的输出:

module Base
MSG_HASH = {0x30=>:One, 0x31=>:Two, 0x32=>:Three}

class Decoder
def decode(wire)
msg_type = self.class::MSG_HASH[wire]
send("decode_#{msg_type}_message".to_sym)
end
def decode_One_message
"This was a One message"
end
def decode_Two_message
"This was a Two message"
end
end
end

module Special
class Decoder < Base::Decoder
MSG_HASH = Base::MSG_HASH.merge({0x4a => :Foo, 0x73 => :Bar})
def decode_Foo_message
"Specialized Foo message"
end
def decode_Bar_message
"Specialized Bar message"
end
end
end

module AnotherSpecial
class Decoder < Base::Decoder
MSG_HASH = Base::MSG_HASH.merge({0x4a => :Zippity, 0x73 => :DooDaa})
def decode_Zippity_message
"Zippity"
end
def decode_DooDaa_message
"DooDaa"
end
end
end

puts "Decoding the first protocol"
[0x30, 0x31, 0x4A, 0x73].each do |wire|
decoder = Special::Decoder.new
puts "(#{wire}) decoded to '#{decoder.decode(wire)}'"
end

puts "Decoding the second protocol"
[0x30, 0x31, 0x4A, 0x73].each do |wire|
decoder = AnotherSpecial::Decoder.new
puts "(#{wire}) decoded to '#{decoder.decode(wire)}'"
end

免责声明:我只在 JRuby 中测试过,没有在 MRI 中测试过。

关于ruby - 从未知模块访问值,委托(delegate)给未知方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com