Manmeet
Manmeet

Reputation: 281

Understanding each_pair and map in ruby

I am currently reviewing some code and i would like to understand what this particular definition is trying to do.

def self.object_to_properties_container object
{
  'properties' => object.each_pair.map do |name, value|
    {'property' => name, 'value' => value}
  end
}
end

Thanks!

Upvotes: 2

Views: 1659

Answers (1)

Grych
Grych

Reputation: 2901

This method will transform your Hash into another hash, which has one key: "properties" and this key contains another Hash of keys: "property" and "value", where "property" contains an original Hash key, and "value" - its value. Hard to elaborate, but easy with an example:

object_to_properties_container({ one: 1, two: 2})
#=> {"properties"=>
  [{"property"=>:one, "value"=>1}, {"property"=>:two, "value"=>2}]}

Upvotes: 1

Related Questions