理解 Ruby 中的点击 [英] Understanding tap in Ruby

查看:41
本文介绍了理解 Ruby 中的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Rails 项目中的一段代码,并发现了 tap 方法.它有什么作用?

另外,如果有人能帮助我理解其余代码的作用,那就太好了:

def self.properties_container_to_object properties_container{}.tap do |obj|obj['vid'] = properties_container['vid'] 如果 properties_container['vid']obj['canonical-vid'] = properties_container['canonical-vid'] 如果 properties_container['canonical-vid']properties_container['properties'].each_pair do |name, property_hash|obj[name] = property_hash['value']结尾结尾结尾

谢谢!

解决方案

.tap 在这里对方法链中的中间结果执行操作"(引用 ruby​​-doc).

换句话说,object.tap 允许你操作 object 并在块之后返回它:

{}.tap{ |hash|hash[:video] = 'Batmaaaaan' }# =>返回具有等于Batmaaaaan"的键/值视频的哈希值本身

所以你可以用 .tap 做这样的事情:

{}.tap{ |h|h[:video] = 'Batmaaan' }[:video]# =>返回 "Batmaaan"

相当于:

h = {}h[:video] = '蝙蝠侠'返回 h[:video]

一个更好的例子:

user = User.new.tap{ |u|u.generate_dependent_stuff }# user 等于 User 的实例,不等于 `u.generate_dependent_stuff` 的结果

<小时>

您的代码:

def self.properties_container_to_object(properties_container){}.tap do |obj|obj['vid'] = properties_container['vid'] 如果 properties_container['vid']obj['canonical-vid'] = properties_container['canonical-vid'] 如果 properties_container['canonical-vid']properties_container['properties'].each_pair do |name, property_hash|obj[name] = property_hash['value']结尾结尾结尾

正在返回一个填充在 .tap 块中的哈希值

您的代码的长版本将是:

def self.properties_container_to_object(properties_container)哈希 = {}hash['vid'] = properties_container['vid'] if properties_container['vid']hash['canonical-vid'] = properties_container['canonical-vid'] if properties_container['canonical-vid']properties_container['properties'].each_pair do |name, property_hash|hash[name] = property_hash['value']结尾散列结尾

I am reviewing a piece of code from a Rails project and I came across the tap method. What does it do?

Also, it would be great if someone could help me understand what the rest of the code does:

def self.properties_container_to_object properties_container
  {}.tap do |obj|
  obj['vid'] = properties_container['vid'] if properties_container['vid']
  obj['canonical-vid'] = properties_container['canonical-vid'] if   properties_container['canonical-vid']
  properties_container['properties'].each_pair do |name, property_hash|
  obj[name] = property_hash['value']
  end
 end
end

Thanks!

解决方案

.tap is here to "perform operations on intermediate results within a chain of methods" (quoting ruby-doc).

In other words, object.tap allows you to manipulate object and to return it after the block:

{}.tap{ |hash| hash[:video] = 'Batmaaaaan' }
# => return the hash itself with the key/value video equal to 'Batmaaaaan'

So you can do stuff like this with .tap:

{}.tap{ |h| h[:video] = 'Batmaaan' }[:video]
# => returns "Batmaaan"

Which is equivalent to:

h = {}
h[:video] = 'Batmaaan'
return h[:video]

An even better example:

user = User.new.tap{ |u| u.generate_dependent_stuff }
# user is equal to the User's instance, not equal to the result of `u.generate_dependent_stuff`


Your code:

def self.properties_container_to_object(properties_container)
  {}.tap do |obj|
    obj['vid'] = properties_container['vid'] if properties_container['vid']
    obj['canonical-vid'] = properties_container['canonical-vid'] if   properties_container['canonical-vid']
    properties_container['properties'].each_pair do |name, property_hash|
      obj[name] = property_hash['value']
    end
  end
end

Is returning a Hash beeing filled in the .tap block

The long-version of your code would be:

def self.properties_container_to_object(properties_container)
  hash = {}

  hash['vid'] = properties_container['vid'] if properties_container['vid']
  hash['canonical-vid'] = properties_container['canonical-vid'] if   properties_container['canonical-vid']
  properties_container['properties'].each_pair do |name, property_hash|
    hash[name] = property_hash['value']
  end

  hash
end

这篇关于理解 Ruby 中的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆