hash - A more elegant way to map an array of hashes in ruby -
i've got array of hashes: hashes = [{field: 'one'}, {field: 'two'}]
and want list of fields it: ['one', 'two']
hashes.map(&:field)
doesn't work, clearly, , hashes.map { |hash| hash[field] }
feels bit clunky me.
is there more elegant way?
edit: should clarify, want value of 'fields' in response.
so, hashes = [{field: 'one', another: 'three'}, {field: 'two'}].do_the_thing
should ['one', 'two']
perhaps next more pleasing eye:
hashes = [{field: 'one', another: 'three'}, {field: 'two'}] fields = lambda { |hash| hash[:field] } hashes.collect(&fields)
ruby hash
No comments:
Post a Comment