From: "David A. Black" Date: 2009-06-23T04:13:11+09:00 Subject: Re: map shall not return an Enumerator ( was re guru help ) Hi -- On Tue, 23 Jun 2009, Robert Dober wrote: > On Mon, Jun 22, 2009 at 2:52 PM, Brian Candler wrote: >> Robert Dober wrote: >> That I dislike very much. What you want is to run a 'join' operation on >> *each member* of the collection, but that looks like running a .map.join >> on the *whole* collection. From that point of view, >> >> coll.map { |c| c.join(",") } >> >> expresses very clearly what you're doing. > > I agree with you, that this is confusing at first sight, but actually > coll.map.join instead of coll.join does not make any sense at all. > My corollary is: > Any method sent to map makes only sense to be sent to the elements of > the collection and not > to the collection itself because that would make map a NOP. You don't send a message to map, though. map is a method; messages go to objects. Methods can be provided with code blocks, but that's part of the method call. Once the next dot appears, the method call is over and the message goes to the resulting object. > I believe that the confusion arises from the fact that map returns an > Enumerator and that just seems quite flawed at second thought (or is > this third thought ;). > > Why the heck does map return an Enumerator? If I wanted that I surely > would have called to_enum ! I can't think of any real use case for map returning an enumerator. It's true that you can do: array.map.with_index {|e,i| ... } but that's because enumerators have a with_index method (one of the very few methods they have that aren't from Enumerable). So you'd be able to do that no matter how you got the enumerator. The returning of an enumerator is more useful with certain other methods. For example: e = array.each_cons(2) Now if you iterate over e, you'll get the each_cons(2) behavior. > And if the receiver already was an Enumerator I want to call map for > some purpose too. You can do that: array.each_cons(2).map {|one,two| ... } or whatever. (Is that what you meant?) David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com