Pe, Botp wrote:
> nobuyoshi nakada 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]
>

i=-1;[1,2,3,4,5,6].map{|x|[2,5].include?(i+=1) ? x : x*2}
#=> [2, 4, 3, 8, 10, 6]

You can, of course, use this idiom to emulate *_with_index
functionality for (almost?) any iterator method.


daz
-- 

Never seek simplicity where complexity will suffice.
 - (paraphrase ;-)