From: Yukihiro Matsumoto Date: 2005-07-08T23:02:11+09:00 Subject: Re: accessing index inside map Hi, In message "Re: accessing index inside map" on Fri, 8 Jul 2005 19:20:48 +0900, "Robert Klemme" writes: |> #$ ruby -renumerator -e 'p |> #(1..6).enum_for(:each_with_index).map{|x,i|[2,5].include?(i) ? |> # |> #[2, 4, 3, 8, 10, 6] |> |> Hi Nobu, |> |> Wow, cool. But my brain is very tiny compared to other rubyist brains. |> |> i was hoping for something straight like, |> |>> [1,2,3,4,5,6].map{|x,i|[2,5].include?(i) ? x : x*2} |> =>[2, 4, 3, 8, 10, 6] |> |> Maybe you can hack one for dumb rubyists like me. | |Err, are you kidding? Enumerator isn't really that difficult - and you |don't need to modify Hash or other classes. It's a simple wrapper |delegating each to each_with_index. That simple. We have vague plan to make enumerating method to return Enumerator when no block is given in the future, so that require 'enumerator' (1..6).enum_for(:each_with_index).map{|x,i|[2,5].include?(i) ? x : x*2} would be (1..6).each_with_index.map{|x,i|[2,5].include?(i) ? x : x*2} then. It's much simpler isn't it? matz.