dblack@wobblini 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.
#
Yes, you are correct. But there is some sort of give and take here or
balance if i may say (i think i heard the word balance fr you, sir David)..
How about
[1,2,3,4,5,6].map(WITH_INDEX) {|x,i|[2,5].include?(i) ? x : x*2}
or
[1,2,3,4,5,6].map(with_index=>true) {|x,i|[2,5].include?(i) ? x : x*2}
if we don't pass flags or attribs, wouldn't that make creating additional
methods like
each
each_with_index
map
map_with_index
collect
collect_with_index
...
?
it's with_index es all over..
Also, if we create a with_index method, wouldn't that be invoking another
method just for the index, like
each_with_index.map
or
map.with_index
Wouldn't my algorithm slow down? Does not the chain begs for a single method
-which brings us back to the original question...?
kind regards -botp
#David