Why Call
Why do we use call
as the default main method for ruby services?
class MyService
def self.call
new.do_stuff
end
def do_stuff
"doing some stuff"
end
end
MyService.call # => "doing some stuff"
Why not run
? or main
?
Well, this is just a ruby convention. This is also enforced in the language at different places.
For example when you grab a method from any Object and try to call it by itself
def foo
"foo"
end
m = Object.new.method(:foo)
puts m.call
# >> foo
Another example, when you create a proc
, one of the ways to use that proc
is to call
it.
p = proc { "foo" }
p.call # => "foo"