Ruby Rack call: class vs instance method?

271 Views Asked by At

I'm using Rack to build a REST API.

Should call be a class or an instance method?

2

There are 2 best solutions below

0
ma11hew28 On
0
Holin On

In fact it does not matter.

#run from config.ru wants an object which responds to #call and that takes one argument.

A class is an object so it can be used as a Rack application.

BUT

As your application will evolve you will likely want to inject some dependencies in your application maybe a database connection :

config.ru ex :

database    = Database.new
application = Application.new(database)
run application

Therfore using an instance would be a better choice, it will be easier to maintain and to test. Most of the times in OOP you should use instances.