Understanding tap in Ruby

前端 未结 2 792

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

相关标签:
2条回答
  • 2021-01-05 09:39

    Tap is a Ruby method from the Object class.

    This method yields x to the block and then returns x. This method is used to "tap into" a method chain, to perform operations on intermediate results within the chain.

    0 讨论(0)
  • 2021-01-05 09:57

    .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
    
    0 讨论(0)
提交回复
热议问题