How should I call object methods from a static method in a ruby class?

Viewed 1036

From an academic / "for interests sake" (best practice) perspective:

In a Ruby class, I want to provide a static method that calls instance methods of the class. Is this considered an acceptable way of doing it, or is there is 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() # <-- is this ok?
      util.apiClient.call("foo", param2)
      # some logic and more util.apiClient.calls() re-use client.
    end
end

And then I can use this lib easily:

MyUtil.setUpSomething("yes","blue")

vs

MyUtil.new().setupUpSomething()
# or
util = MyUtil.new()
util.setUpSomething()

The environment is sys admin scripts that get executed in a controlled manner, 1 call at a time (i.e. not a webapp type of environment that could be subjected to high load).

3 Answers
Related