Best way to split arrays into multiple small arrays in ruby

后端 未结 4 536
别跟我提以往
别跟我提以往 2020-12-17 09:47

What is the simplest way to split arrays into multiple arrays based on some conditions? In my scenario, I need to move the integer and the string values to different arrays.

4条回答
  •  一整个雨季
    2020-12-17 10:36

    You're looking for Enumerable#partition:

    x = [1, 2, 3, "a", "b", 4]
    numbers, not_numbers = x.partition{|item| item.kind_of?(Fixnum)}
    # => [[1, 2, 3, 4], ["a", "b"]]
    

提交回复
热议问题