问题
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'
This is the offending line:
@connection.send(Protocol::Queue::Bind.encode(@channel.id, @name, exchange_name, routing_key, nowait, arguments))
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).
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'.
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?
FYI: I'm using Ruby 1.9.2p180 on OSX.
回答1:
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.
来源:https://stackoverflow.com/questions/6139942/ruby-failing-to-override-send-method