1

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.

dreftymac
  • 31,404
  • 26
  • 119
  • 182

2 Answers2

3

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
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
  • AFAICT, this is not equivalent, because Data::Rmap works with more than just ArrayOfHash (AoH). It can also iterate more complex structures such as ArrayofArray (AoA), HashofArray (HoA), and others (AoAoH), (HoHoA) and so forth ad-infinitum. – dreftymac Jan 07 '09 at 16:24
  • .map will happily work on Arrays of things other than Hash. What it won't do is recursion. Perhaps a Y-combinator is in order? – James A. Rosen Jan 07 '09 at 21:21
1

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.

Keltia
  • 14,535
  • 3
  • 29
  • 30