From: Jason Roelofs Date: 2008-01-04T09:06:29+09:00 Subject: Re: Troubles with blocks ------=_Part_35941_3578793.1199405190981 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Jan 3, 2008 6:49 PM, Holden Holden wrote: > Hi, > > I am a Python programmer and I've just started learning Ruby. Until now, > I've enjoyed it, but I am having troubles with some methods that require > blocks. > > This is a simple example that (I expect) summarizes my problems - how to > obtain a list of pairs [index, value] (or [value, index], it doesn't > matter) from an array: > > Python: > > list(enumerate(['a','b','c'])) > => [(0, 'a'), (1, 'b'), (2, 'c')] > > Ruby: > > t = []; ['a','b','c'].each_with_index { |*x| t << x }; t > => [["a", 0], ["b", 1], ["c", 2]] > > Which is a very ugly solution. How could I do this on Ruby without a > temporal variable? > > thanks > -- > Posted via http://www.ruby-forum.com/. > > Hmm, I can't find a shorter way than what you've got. Why are you trying to get this kind of information? Maybe there's something earlier in your code that can be made more Ruby-esque to make this easier for you. Jason ------=_Part_35941_3578793.1199405190981--