Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding tap in Ruby

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!

like image 985
Manmeet Avatar asked Aug 07 '14 20:08

Manmeet


1 Answers

.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
like image 159
MrYoshiji Avatar answered Oct 05 '22 03:10

MrYoshiji