How can I properly chain custom methods in Ruby?

后端 未结 4 2014
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 10:49

I am trying to do a chaining method for the following two methods. After running this code, I kept getting the following output:

#

        
4条回答
  •  不思量自难忘°
    2021-02-08 11:23

    Another way is to build pipeline via chainable_methods gem.

    Described in the article

    require 'chainable_methods'
    
    module SimpleMath
      include ChainableMethods
    
      def add(a, b=0)
        a + b
      end
    
      def subtract(a, b=0)
        a - b    
      end
    end
    
    SimpleMath.
      chain_from(5).
      add(5).
      add(5).
      subtract(3).
      unwrap
    

提交回复
热议问题