0

在下面的代码中,我遇到了一个错误,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错了吗?

4

1 回答 1

2

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

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已经存在,这也可以工作......这就是红宝石的工作方式......

于 2014-06-26T19:30:10.667 回答