简体   繁体   English

红宝石,rails,常量,resque

[英]ruby, rails, constants, resque

I have a bunch of classes that are resque jobs and I just noticed I have a constant defined in each named RECEIVER which contains the email distribution list for the jobs results. 我有一堆类是可重用的作业,我只是注意到我在每个名为RECEIVER的对象中都有一个常量,其中包含用于作业结果的电子邮件分发列表。

What is the default behavior in ruby/rails if I have a constant RECEIVER = "emails" and have it defined in multiple classes. 如果我有一个常量RECEIVER =“ emails”并将它定义在多个类中,那么ruby / rails中的默认行为是什么? Each class assigns the value of RECEIVER to an instance of the class upon initialization. 每个类在初始化时都会将RECEIVER的值分配给该类的实例。

Just trying to think of the best way to refactor something like this. 只是想想重构这种东西的最佳方法。 thank you 谢谢

It gets defined in each class separately. 它在每个类中分别定义。 The best way to refactor to prevent code duplication would be to use a module 重构以防止代码重复的最佳方法是使用module

module CommonMethods
   RECEIVER = "emails"
end

and then in your class: 然后在你的课上:

class SomeClass
  include CommonMethods

  #do stuff
end

That way the email list is only defined in one place. 这样,电子邮件列表仅在一个地方定义。

You could also define other methods in here that are common to all of your classes. 您还可以在此处定义所有类通用的其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM