advantage of tap method in ruby

后端 未结 19 1012
天命终不由人
天命终不由人 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:55

    In rails we can use tap to whitelist parameters explicitly:

    def client_params
        params.require(:client).permit(:name).tap do |whitelist|
            whitelist[:name] = params[:client][:name]
        end
    end
    

提交回复
热议问题