In Ruby, is there an Array method that combines 'select' and 'map'?

后端 未结 14 794
盖世英雄少女心
盖世英雄少女心 2020-12-07 18:34

I have a Ruby array containing some string values. I need to:

  1. Find all elements that match some predicate
  2. Run the matching elements through a transfo
14条回答
  •  囚心锁ツ
    2020-12-07 19:19

    I'm not sure there is one. The Enumerable module, which adds select and map, doesn't show one.

    You'd be required to pass in two blocks to the select_and_transform method, which would be a bit unintuitive IMHO.

    Obviously, you could just chain them together, which is more readable:

    transformed_list = lines.select{|line| ...}.map{|line| ... }
    

提交回复
热议问题