From: Robert Klemme Date: 2005-07-08T19:20:48+09:00 Subject: Re: accessing index inside map Pe�a, Botp wrote: > nobuyoshi nakada [mailto:nobuyoshi.nakada@ge.com] wrote: > > #> i'd like to double the element values except those loc on > #the 2nd and 5th > #> indices eg. > # > #$ ruby -renumerator -e 'p > #(1..6).enum_for(:each_with_index).map{|x,i|[2,5].include?(i) ? > #x : x*2}' > #[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. Regards robert