conditional chaining in ruby

后端 未结 9 2180
一生所求
一生所求 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 20:57

    You can use tap:

    my_object.tap{|o|o.method_a if a}.tap{|o|o.method_b if b}.tap{|o|o.method_c if c}
    

提交回复
热议问题