ruby Object#tap and Object#returning

Posted by wxianfeng Wed, 19 Jan 2011 03:29:00 GMT

今天在rails中使用 returning 的时候 log 打出warning:

Object#returning has been deprecated in favor of Object#tap

环境是 ruby 1.9.2 + rails 3.0.3

从warn上看是returning不建议使用,建议使用tap方法,那么tap方法和returning方法有什么不同

Object#tap 方法是 ruby1.8.7 以后加入的,Object#returning 方法是 rails添加的

rails 3.0.3 returning 源码:here

rails 2.3.5 returning源码:

class Object
  # Returns +value+ after yielding +value+ to the block. This simplifies the
  # process of constructing an object, performing work on the object, and then
  # returning the object from a method. It is a Ruby-ized realization of the K
  # combinator, courtesy of Mikael Brockman.
  #
  # ==== Examples
  #
  #  # Without returning
  #  def foo
  #    values = []
  #    values << "bar"
  #    values << "baz"
  #    return values
  #  end
  #
  #  foo # => ['bar', 'baz']
  #
  #  # returning with a local variable
  #  def foo
  #    returning values = [] do
  #      values << 'bar'
  #      values << 'baz'
  #    end
  #  end
  #
  #  foo # => ['bar', 'baz']
  #  
  #  # returning with a block argument
  #  def foo
  #    returning [] do |values|
  #      values << 'bar'
  #      values << 'baz'
  #    end
  #  end
  #  
  #  foo # => ['bar', 'baz']
  def returning(value)
    yield(value)
    value
  end

  # Yields <code>x</code> to the block, and then returns <code>x</code>.
  # 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}" }
  def tap
    yield self
    self
  end unless Object.respond_to?(:tap)

  # An elegant way to factor duplication out of options passed to a series of
  # method calls. Each method called in the block, with the block variable as
  # the receiver, will have its options merged with the default +options+ hash
  # provided. Each method called on the block variable must take an options
  # hash as its final argument.
  # 
  #   with_options :order => 'created_at', :class_name => 'Comment' do |post|
  #     post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
  #     post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
  #     post.has_many :all_comments
  #   end
  #
  # Can also be used with an explicit receiver:
  #
  #   map.with_options :controller => "people" do |people|
  #     people.connect "/people",     :action => "index"
  #     people.connect "/people/:id", :action => "show"
  #   end
  #
  def with_options(options)
    yield ActiveSupport::OptionMerger.new(self, options)
  end
  
  # A duck-type assistant method. For example, Active Support extends Date
  # to define an acts_like_date? method, and extends Time to define
  # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
  # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
  # we want to act like Time simply need to define an acts_like_time? method.
  def acts_like?(duck)
    respond_to? "acts_like_#{duck}?"
  end

end

可以看到 tap 方法也封装了,为了防止 ruby版本过低 没有tap方法就 添加Object#tap 方法,tap 和 returning本质是一样的,函数体都是调用block闭包,只不过returning需要传递一个参数给闭包,最后返回的就是这个参数,而 tap直接操作self指针,最后返回的也就是self

另外最新rails源码(>rails3.0.3)已经没有 returning方法了,所以以后最好都用tap方法

DEMO1:

require "rubygems"
require "active_support"
# Object#tap 是>ruby1.8.7 有的
# Object#returning 是Rails 封装的方法, rails3.X 已经不建议使用

# Object#tap 可以支持链式(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}"
}

def object_tap
  {}.tap do |h| # => Hash.new.tap
    h[:a] = 1
    h[:b] =2
  end
end

p object_tap # {:b=>2, :a=>1}

def object_returning
  returning Hash.new do |h| # 注意这里不能用 {}  , 放在 returning 方法后面 当作成 block闭包了
    h[:a] = 1
    h[:b] = 2
  end  
end

p object_returning # {:b=>2, :a=>1}

DEMO2:

require "rubygems"
require "active_support"

class Hash  
  def shift_value_tap_self
    self.tap do |h|
      h.each { |k,v| v.shift if v.is_a?(Array) }
    end
  end
  def shift_value_tap
    {}.tap do |h|
      self.each { |k,v|  v.is_a?(Array) ? h[k] = v.shift : h[k] = v } 
    end
  end

  def shift_value_returning
    returning Hash.new do |h|
      self.each { |k,v|  v.is_a?(Array) ? h[k] = v.shift : h[k] = v } 
    end
  end
  def shift_value_returning_self
    returning self do |h|
      h.each { |k,v| v.shift if v.is_a?(Array) }
    end
  end
end

hsh = {"a"=>[1,2,3],"b"=>["g","f","w"],"c"=>"fuck_china"}
hsh1 = {"a"=>[1,2,3],"b"=>["g","f","w"],"c"=>"fuck_china"}
hsh2 = {"a"=>[1,2,3],"b"=>["g","f","w"],"c"=>"fuck_china"}
hsh3 = {"a"=>[1,2,3],"b"=>["g","f","w"],"c"=>"fuck_china"}
p hsh.shift_value_tap # {"a"=>1, "b"=>"g", "c"=>"fuck_china"}
p hsh1.shift_value_returning # {"a"=>1, "b"=>"g", "c"=>"fuck_china"}
p hsh2.shift_value_tap_self # {"a"=>[2, 3], "b"=>["f", "w"], "c"=>"fuck_china"}
p hsh3.shift_value_returning_self # {"a"=>[2, 3], "b"=>["f", "w"], "c"=>"fuck_china"}

SEE:
http://blog.rubybestpractices.com/posts/gregory/011-tap-that-hash.html
http://www.simonecarletti.com/blog/2010/09/rails-3-beware-the-tap-pattern/
http://fuliang.javaeye.com/blog/857163

This entry was posted on Wed, 19 Jan 2011 03:29:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=173

Comments

Leave a comment