From: Robert Feldt Date: 2001-10-11T08:33:51+09:00 Subject: [ruby-talk:22407] Re: list comprehensions alike Python ??? On Thu, 11 Oct 2001, markus jais wrote: > hello > Hi, > a few days ago, I bought the "Python 2.1 Bible" > there (on page 51) is a cool feature described called > "list comprehension" > > it works like this: > > with python code like this: > > l = [x * x for x in range(1, 11)] > > I get a list with squares of all numbers of 1-10 > > in ruby I could do this with the following code: > > a = Array.new > (1..10).each { |elem| > a[elem-1] = elem * elem > } > > is there an even shorter way do to this in ruby?? > $ irb irb(main):001:0> (1..11).map {|v| v*v} [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121] Short enough for my taste... (but maybe there are some benefits to the Python way? I don't know any Python.) Regards, /Robert