From: "David A. Black" Date: 2005-07-08T19:40:44+09:00 Subject: Re: accessing index inside map --927295978-1658795860-1120819221=:24874 Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-1658795860-1120819221=:24874" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --927295978-1658795860-1120819221=:24874 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Fri, 8 Jul 2005, Robert Klemme wrote: > Pe=F1a, 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} >> =3D>[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. I'd still like to see #man_with_index added to Enumerable. David --=20 David A. Black dblack@wobblini.net --927295978-1658795860-1120819221=:24874-- --927295978-1658795860-1120819221=:24874--