Best way to split arrays into multiple small arrays in ruby

后端 未结 4 560
别跟我提以往
别跟我提以往 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:34

    Here is my solution:

    hash = x.group_by { |t| t.kind_of? Fixnum }
    # hash  => {true=>[1, 2, 3, 4], false=>["a", "b"]} 
    array1 = hash[true] # The array of integers
    array2 = hash[false] # The array of strings
    

提交回复
热议问题