I try to perform this very simple RUBY map statement:
result_apartments = [{:id=>"593363170", :title=>"Country House", :price=>{:cents=>30000000, :currency=>"EUR", :human=>"€300,000.00"}, :sqm=>130, :numberOfBedrooms=>30, :numberOfBathrooms=>1}, {:id=>"906270510", :title=>"City Flat", :price=>{:cents=>80000000, :currency=>"USD", :human=>"$800,000.00"}, :sqm=>60, :numberOfBedrooms=>5, :numberOfBathrooms=>23}]
puts result_apartments.map(&:id)
Somehow it throws the error: map': undefined method id' for #<Hash:0x0055d52
What is the issue here? What is wrong in the array presentation? And how should I change it so that I can map it in this short cut version: map(&:id)
Somehow (map{|a| a[:id]}) works!
the short method is attempting to call a method id
on each hash. That's not a hash method. If you do...
result_apartments.first.id
You'll see that id
isn't a valid method.
Instead of hashes you could have an array of OpenStruct
require 'ostruct'
structured_result_apartments = result_paraments.map { |hash| OpenStruct.new hash }
Then you can do
structured_result_apartments.map(&:id)