List comprehension in Ruby

后端 未结 17 2207
广开言路
广开言路 2020-12-02 07:01

To do the equivalent of Python list comprehensions, I\'m doing the following:

some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}

Is ther

17条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 07:55

    https://rubygems.org/gems/ruby_list_comprehension

    shameless plug for my Ruby List Comprehension gem to allow idiomatic Ruby list comprehensions

    $l[for x in 1..10 do x + 2 end] #=> [3, 4, 5 ...]
    

提交回复
热议问题