Combinatory method like tap, but able to return a different value?

前端 未结 5 1721
时光取名叫无心
时光取名叫无心 2020-12-24 06:00

I\'m going through a phase of trying to avoid temporary variables and over-use of conditional where I can use a more fluid style of coding. I\'ve taken a great liking to us

5条回答
  •  抹茶落季
    2020-12-24 07:02

    def best_nice_method
      something_complex(a, b, c).tap |obj|
        break obj.one_more_method_call if a_predicate_check?
      end
    end
    

    The magic is break in tap returns another value.

    new

    ruby 2.5 has yield_self which exactly you want. https://stackoverflow.com/a/47890832/683157

提交回复
热议问题