From academic / "for the sake of interests" (best practice):
In the Ruby class, I want to provide a static method that calls the methods of an instance of the class. Is this an acceptable way to do this, or is there a "better" way?
class MyUtil
def apiClient
@apiClient ||= begin
apiClient = Vendor::API::Client.new("mykey")
end
end
def self.setUpSomething(param1, param2, param3=nil)
util = self.new()
util.apiClient.call("foo", param2)
end
end
And then I can easily use this lib:
MyUtil.setUpSomething("yes","blue")
vs
MyUtil.new().setupUpSomething()
util = MyUtil.new()
util.setUpSomething()
Environment - these are sys admin scripts that are executed in a controlled manner, 1 call at a time (i.e. not a webapp type that can be subject to high load).
source
share