Ruby无法覆盖send方法 [英] Ruby failing to override send method

查看:71
本文介绍了Ruby无法覆盖send方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点难以解释,但是我似乎有时无法覆盖应用程序中的'send'方法.我正在基于EventMachine创建一个相当大的应用程序,有时,在代码深处,我决定在我的一个类中定义一个发送"方法.以后当我尝试使用此方法时,通常会出现类似TypeError: <parameter> is not a symbol的异常,如下所示(可能是由所需的AMQP gem(不是我的)引起的,但是这个问题更笼统): /p>

This is a bit hard to explain, but I seem to be occasionally unable to override the 'send' method in my application. I'm creating a fairly large application based on EventMachine and sometimes, deep in the bowels of my code, I decide to define a 'send' method in one of my classes. When I later attempt to use this method, I normally get an exception that looks something like TypeError: <parameter> is not a symbol, such as the following (happens to be caused by the required AMQP gem (not mine), but this question is more general):

Exception caught: TypeError - #<AMQ::Protocol::MethodFrame:0x000001008434f0 @payload="\x002\x00\x14\x00\x00\x01a\tamq.topic\x00\x01\x00\x00\x00\x00", @channel=1> is not a symbol
/Users/mlartz/.rvm/gems/ruby-1.9.2-p180@rflow-component-devel/gems/amq-client-0.7.0.alpha27/lib/amq/client/queue.rb:137:in `bind'
/Users/mlartz/.rvm/gems/ruby-1.9.2-p180@rflow-component-devel/gems/amqp-0.8.0.rc12/lib/amqp/queue.rb:282:in `block in bind'
/Users/mlartz/.rvm/gems/ruby-1.9.2-p180@rflow-component-devel/gems/eventmachine-1.0.0.beta.3/lib/em/deferrable.rb:47:in `call'
/Users/mlartz/.rvm/gems/ruby-1.9.2-p180@rflow-component-devel/gems/eventmachine-1.0.0.beta.3/lib/em/deferrable.rb:47:in `callback'

这是违规行:

@connection.send(Protocol::Queue::Bind.encode(@channel.id, @name, exchange_name, routing_key, nowait, arguments))

在这种特殊情况下,@connection对象的类定义了一个接受AMQ::Protocol::MethodFramesend方法.但是,似乎以某种方式调用了默认的Object#send方法(它需要一个符号,因此是例外).

In this particular case, the @connection object's class has defined a send method that takes in an AMQ::Protocol::MethodFrame. It seems, however, that somehow the default Object#send method is getting called (which expects a symbol, hence the exception).

在开发的早期,我在一个自定义类中遇到了同样的问题,可以通过将"send"方法的名称更改为"send_message"来解决.

Earlier in development, I had the same issue on one of my custom classes, which was solved by changing the name of my 'send' method to 'send_message'.

那么,这有点笼统,问题是,什么样的事情会干扰我在定义该对象的对象上调用自定义send方法的能力?

So, as this is a bit general, the question is, what sorts of things could interfere with my ability to call a custom-defined send method on the object that defined it?

仅供参考:我正在OSX上使用Ruby 1.9.2p180.

FYI: I'm using Ruby 1.9.2p180 on OSX.

推荐答案

建议不要使用或覆盖send方法. Object.send 是Ruby 内核模块.定义Message.send类时,我曾经遇到过严重的问题,并最终使用了不同的名称,例如Message.transmit.在Ruby on Rails中,它是一个保留字.

It is not recommendable to use or override the send method. Object.send is a very basic method in the Ruby Kernel Module. I once got heavy problem when I defined a Message.send class, and ended up in using a different name, such as Message.transmit. In Ruby on Rails it is a reserved word.

这篇关于Ruby无法覆盖send方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆