zoukankan      html  css  js  c++  java
  • 使用Object#tap使代码更优雅

    今天看spree源码的时候经常看到Object#tap方法。以前只知道有这个方法,而且感觉这个方法调试的作用大于实际,今日看来以前的理解应该不够准确。

    先看下官方文档上tap的例子

    Yields self to the block, and then returns self. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

    (1..10)                .tap {|x| puts "original: #{x.inspect}"}
      .to_a                .tap {|x| puts "array: #{x.inspect}"}
      .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
      .map {|x| x*x}       .tap {|x| puts "squares: #{x.inspect}"}
    
    

    tap在block里访问自身,然后迭代结束后返回自身,这样很容易插入方法链中。有点像是先打开caller,然后对caller进行操作,最后返回caller。

    举个例子比较一下使用tap会对代码带来什么好处

    
    require 'ostruct'
    require 'pp'
    
    eason = OpenStruct.new
    eason.name = 'eason'
    eason.age = 30
    eason.sex = 'male'
    
    def duplicate(obj)
      new_obj = obj.dup
      new_obj.name = 'new name'
      new_obj.age = 18
      new_obj
    end
    
    def duplicate_with_tap(obj)
      obj.dup.tap do |new_obj|
        new_obj.name = 'new name'
        new_obj.age = 18
      end
    end
    
    pp duplicate(eason)
    pp duplicate_with_tap(eason)
    
    

    可以看到,duplicate方法的最后一行需要显示的返回new_obj,而duplicate_with_tap方法中,tap会自动返回自身,这样整个方法显得浑然一体,更加优雅。

  • 相关阅读:
    mysql
    关于的字符串的总结(群,子群,KMP算法,正则表达式):
    正则表达式中[],,B的用法:
    链表
    numpy学习笔记
    算法的笔记 -概念性初始
    单线程实现了多任务异步协程
    windows安装redis出现的小问题
    将虚拟机的ip改为静态
    nosql与mysql的区别?
  • 原文地址:https://www.cnblogs.com/nbkhic/p/4575000.html
Copyright © 2011-2022 走看看