您的位置:首页 > 移动开发 > Objective-C

使用Object#tap使代码更优雅

2015-06-14 14:01 204 查看
今天看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
会自动返回自身,这样整个方法显得浑然一体,更加优雅。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: