换句话说,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 endendThanks! 解决方案 .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 endendIs returning a Hash beeing filled in the .tap blockThe 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 hashend 这篇关于理解 Ruby 中的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 02:35