From: Mark Slagell Date: 2001-10-11T09:11:21+09:00 Subject: [ruby-talk:22416] Re: list comprehensions alike Python ??? Paul Prescod wrote: > > Robert Feldt wrote: > > > >... > > $ 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.) > > Out of curiousity, what would the Ruby for this look like: > > >>> arr = [x*x for x in range(0,10) if x%2==0] > >>> arr > [0, 4, 16, 36, 64] > > A combination of filter and map? > > Paul Prescod A couple of ways spring to mind. (0..10).select{|x| x%2==0}.map{|x| x*x} or (0..10).map{|x| x*x if x%2==0}.compact