advantage of tap method in ruby

后端 未结 19 1111
天命终不由人
天命终不由人 2020-12-22 16:50

I was just reading a blog article and noticed that the author used tap in a snippet something like:

user = User.new.tap do |u|
  u.username = \         


        
19条回答
  •  长情又很酷
    2020-12-22 17:56

    Visualize your example within a function

    def make_user(name)
      user = User.new
      user.username = name
      user.save!
    end
    

    There is a big maintenance risk with that approach, basically the implicit return value.

    In that code you do depend on save! returning the saved user. But if you use a different duck (or your current one evolves) you might get other stuff like a completion status report. Therefore changes to the duck might break the code, something that would not happen if you ensure the return value with a plain user or use tap.

    I have seen accidents like this quite often, specially with functions where the return value is normally not used except for one dark buggy corner.

    The implicit return value tends to be one of those things where newbies tend to break things adding new code after the last line without noticing the effect. They do not see what the above code really means:

    def make_user(name)
      user = User.new
      user.username = name
      return user.save!       # notice something different now?
    end
    

提交回复
热议问题