conditional chaining in ruby

后端 未结 9 2179
一生所求
一生所求 2020-12-13 20:30

Is there a good way to chain methods conditionally in Ruby?

What I want to do functionally is

if a && b && c
 my_object.some_method_b         


        
9条回答
  •  心在旅途
    2020-12-13 21:11

    I use this pattern:

    class A
      def some_method_because_of_a
         ...
         return self
      end
    
      def some_method_because_of_b
         ...
         return self
      end
    end
    
    a = A.new
    a.some_method_because_of_a().some_method_because_of_b()
    

提交回复
热议问题