Celluloid 0.17.3 出现意外 "undefined method" 错误

Celluloid 0.17.3 giving unexpected "undefined method" error

我今天早上第一次开始使用 Celluloid gem。我正在关注这个 Railscasts tutorial 并试图解决问题。

我有一个名为 "SomeClass" 的 class,它只有一种方法。这是代码:

require 'celluloid'

class SomeClass
  include Celluloid
  def initialize(name)
    @name = name
  end

  def assholify()
    puts "#{@name} has become an ASSHOLE."
  end
end

当我创建 class 的新实例并调用它的方法时(伴随一声巨响,即 "assholify!"),我收到 undefined method 'assholify!' 错误。但是 Celluloid 应该在用 bang 调用时异步触发该方法。所以这就是我调用方法的方式:

names = ['John', 'Tom', 'Harry']

names.each do |name|
  n = SomeClass.new name
  n.assholify!
end

这是错误的完整回溯:

I, [2016-09-09T11:28:02.488618 #3682]  INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
/home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:42:in `rescue in check': undefined method `assholify!' for #<SomeClass:0x10897dc> (NoMethodError)
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:39:in `check'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:26:in `dispatch'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:16:in `dispatch'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:50:in `block in dispatch'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:76:in `block in task'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:339:in `block in task'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task.rb:44:in `block in initialize'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:14:in `block in create'
    from (celluloid):0:in `remote procedure call'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:45:in `value'
    from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/sync.rb:22:in `method_missing'
    from some_class.rb:18:in `block in <main>'
    from some_class.rb:16:in `each'
    from some_class.rb:16:in `<main>'

为什么会出现此错误?这是调用函数的正确方法吗?另外我如何摆脱 Celluloid 0.17.3 is running in BACKPORTED mode. 警告?

undefined method 错误的发生是因为在最新版本的赛璐珞 gem 中,演员方法没有被调用。相反,您可以这样调用该方法:n.async.assholify。所以代码应该是这样的:

names = ['John', 'Tom', 'Harry']

names.each do |name|
  n = SomeClass.new name
  n.async.assholify    # Instead of "n.assholify!"
end


对于 "Celluloid 0.17.0 is running in BACKPORTED mode" 警告,请查看 this wiki。向后移植模式是默认模式,时间有限。如果您使用 require 'celluloid/current' 而不是 require 'celluloid',您应该不会看到此警告。