简体   繁体   中英

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.

What is the default behavior in ruby/rails if I have a constant RECEIVER = "emails" and have it defined in multiple classes. Each class assigns the value of RECEIVER to an instance of the class upon initialization.

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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