From: Robert Klemme Date: 2005-07-11T20:40:50+09:00 Subject: Re: accessing index inside map David A. Black wrote: > Hi -- > > On Mon, 11 Jul 2005, [iso-8859-1] "Pe�a, Botp" wrote: > >> OK, i'll be shooting for the moon here. How about passing a >> parameter? Is it then possible to make #each or #map same behavior w >> #each_with_index or #map_with_index or whatever *_with_index so that >> the ff works, >> >> def each(with_index=true) do... end >> >> def map(with_index=true) do ... end >> >> and thus, we can do >> >> [1,2,3,4,5,6].map(true) {|x,i|[2,5].include?(i) ? x : x*2} > > I'd hope not to see that. I'm actually hoping that maybe someday all > the boolean flag methods (like instance_methods(true) etc.) will be > changed :-) They've always seemed to me to be the most cryptic thing > in all of Ruby. To second that: it's generally considered not very clean to have a flag change a method's behavior. I think this is a case where another method is superior to having one method behavior switched. Even from an implementation perspective: either you end up with a single method that consists of if-then-else on the top level with one implementation in each branch or you end up with a lot of if-then-else inside the looping. The latter is inefficient and the former is more efficient but then you can have to methods anyway. Kind regards robert