意外结束输入?

时间:2014-06-26 19:25:09

标签: ruby-on-rails ruby class-eval

在以下代码中我遇到了一个错误,指出syntax error, unexpected '\n', expecting :: or '[' or '.' (SyntaxError)但我不知道问题出在哪里。

module Xaaron
  class ApiKey.class_eval # It does not like this....
     include Promiscuous::Publisher
     publish :xaaron_users_id, :api_key, :as => :ApiKey
  end
end

我使用class_eval错了吗?

1 个答案:

答案 0 :(得分:2)

您可以删除class关键字,并在调用class_eval后添加do(传递一个区块):

module Xaaron
  ApiKey.class_eval do
     include Promiscuous::Publisher
     publish :xaaron_users_id, :api_key, :as => :ApiKey
  end
end

(鉴于ApiKey已存在)

或者您可以完全删除class_eval

module Xaaron
  class ApiKey
     include Promiscuous::Publisher
     publish :xaaron_users_id, :api_key, :as => :ApiKey
  end
end

即使ApiKey已经存在,这也会有用......这就是红宝石的工作原理......