Is there a Ruby equivalent to Perl's Data::Rmap?
-
03-07-2019 - |
Question
Perl's Data::Rmap allows you to recursively evaluate a BLOCK over a list of data structures (locally setting $_ to each element) and return the list composed of the results of such evaluations. $_ can be used to modify the elements.
This is useful for iterating over things like nested hashes, or hierarchies of arrays of hashes and the like.
Solution
Ruby's Enumerable
does everything you want, I think. "... and return the list composed of the results of such evaluations" indicates you want Enumerable#map
. My first go would be something like this:
[ {...}, {...}, {...}, ... ].map do |hash|
hash.something
do_other_stuff_with(hash)
hash # important to have as last line b/c of how #map works
end
OTHER TIPS
Without really looking into details, I'm not sure you need a module for that in Ruby. Iterators and blocks are there to do what you want.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow