Is there an API or how would one go about creating hash keys being accessible by dot(.) methods like if there was Array of objects.
Here is an example :
data = [
{
key1: 'value1',
key2: 'value2'
},
{
key1: 'valuex',
key2: 'valuey'
},
...
]
If I tried to do this :
data.collect(&:key1)
Would get this error :
NoMethodError: undefined method `key1' for #<Hash:0x007fc2a7159188>
This however works :
data.collect{|hs| hs[:key1]}
Just because its a symbol and not object property. Is there a way I could accomplish same behaviour with symbols as if they were object properties?
hs[:key1]doesn't call akey1method on the hash but it calls the[]method with:key1as an argument.