From: Guido De Rosa Date: 2010-10-07T20:32:54+09:00 Subject: Re: "map" a deeply nested structure: Object#deep_map Robert Klemme wrote: > Guido De Rosa wrote: >> >> Of course it depends by the usage scenario, and I actually found such a >> need in one of my production projects. I may tell you longer detail if >> you wish... > > Yes, please. > >> Of course there's no need that the values belong to the same class or to >> derived class, they just need to be uniform in the duck-typing sense. >> >> Consider something like: >> >> my_deep_structure.deep_map{|v| v.to_s} > > What do you need that for? If it is for printing or logging then you > can use my_deep_structure.to_s or my_deep_structure.inspect. > >> this more convenient that overwriting to_json, to_yaml, marshal_dump and >> so on... > > You really make me curious about your use case. This is embarrassing but I'm thinking - right now - that I made some other mistakes in "my use case". Thanks for the head-ups, this discussion has proven useful. What I will do, if you're still curious, is just provide a link to my fixes (as git commits, when they are done). Now, don't mind my use case. Let's return to my first post. My conclusion is that the method I wrote should be renamed deep_map_values. And another method may be called deep_rekey with obvious behavior. Hash#map is totally another beast: 1. it mixes key and values in the transformation (which may lead to unwanted results in a hypothetical recursive version IMHO) 2. it turns an Hash into an Array of pairs (which may be what you want, or not) Regards, Guido -- Posted via http://www.ruby-forum.com/.