ruby - Define class with class method which calls new within then calls class instance method with the same name, what pattern is this? -
I am working on a project and look at such patterns, see the snippet below. Which one is commonly used? What is the benefit coding like this? Thanks!
Note: The argument here can be examples of other classes here, not just general values.
square foo def self.do_something arg1, arg2 new (arg1, arg2) .do_something end dff rg start, rg 2 # set instance variable @ var1 = arg1 @ var2 = arg2 end df do_something # Let us do something ... Return Self and End Two examples from my project:
-
Worker class for resque < Pre> class MyWorkers @q = 'my_queue' def self.enqueue (arg1, arg2) new (arg1, arg2). Perform the arguments and def (arg1, arg2) new (arg1, arg2). Perform and def initialize (arg1, arg2) @ var1 = arg1 @ var2 = arg2 end DEP ANQU Rescue.Anquieu (self class, @ var1, @ Var2) Do something with End DRF # var # var1, @ var2 end end -
Return a class and result to run the query < Pre> class MyFinder attr_reader: result def self.execute (params, options = {} new (parameter, options) .execute end def initiailize (params, options) @params = params || {} @options = options End run the exec # query and the result is current variable = result = SomeModel.where (params) and end
This is the one, the following is an example that you have a mirror present there. The example can be found, which it says in the words of Ruby, and goes deeper into it as well as in other patterns as well.
Class Adjustment Matrix Class & lt; & Lt; (DTC), built-in (data) new DIF (data) enddef instructions (data) new (data, true) end personal: new end DEF start (data, guided = false) # end ... end end undirected_matrix = AdjacencyMatrix Undirected (data) instructions_matics = adjectives targeted (data)
Comments
Post a Comment