← #TIL

A service that inherit from BaseService should implement method call

·1 phút đọc·#rails#ruby#concept

Why?

  • Adding Syntactic Sugar to Make Rails Service Objects Suck Less
  • If only there were precedence in Ruby for calling something and having it execute itself immediately with the given parameters… oh wait, there is! It’s Proc#call

What’s Proc#call ?

  • Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Returns the value of the last expression evaluated in the block.
  • Note that proc.() invokes proc.call() with the parameters given. It’s syntactic sugar to hide “call”.
  • For procs created using lambda or -> () an error is generated if the wrong number of parameters are passed to the proc. For procs created using Proc.new or Kernel.proc, extra parameters are silently discarded and missing parameters are set to nil.

Ref:

Ruby’s Eigenclass Rails Service Objects: A Comprehensive Guide Proc#call

Note này hắn lấy từ GitHub issue qua.