Accessing index inside map

#> You're vague plan produces clearer ruby..
#>
#> but isn't
#> each_with_index.map
#>
#> so close to being
#> map_with_index
#>
#> ?
#>
#> (1..6).map_with_index{|x,i|[2,5].include?(i) ? x : x*2} is
#very easy to my
#> tiny brain since all i have to remember is map (just like
#(in array) all i
#> have to focus is each, then i follow thru with each_with_index)

···

nobuyoshi nakada [mailto:nobuyoshi.nakada@ge.com] wrote:

#
#What about Enumerator#with_index?
#
# [1,2,3,4,5,6].map.with_index {|x,i|[2,5].include?(i) ? x : x*2}

imho, #with_index pleads for a better name. It sounds far from being a
method; which leads me to incline back to your original proposition of

.each_with_index.map...

another concern also is that sir matz already warned me of the spilling of
the *_with_index naming all over the place..

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 think this one does not break old code, no?

I find index being more of an attribute than a pure method.

And you are all right, I just need the index, why do i need another set of
methods?

Again, imho.

Thanks and kind regards -botp

#
#--
#Nobu Nakada
#

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 :slight_smile: They've always seemed to me to be the most cryptic thing
in all of Ruby.

David

--
David A. Black
dblack@wobblini.net

David A. Black wrote:

Hi --

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 :slight_smile: 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

···

On Mon, 11 Jul 2005, [iso-8859-1] "Peña, Botp" wrote: