ruby array (enumerable) method to select and reject into 2 arrays in 1 operation -


# code works list = (0..20).to_a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]   odd = list.select { |x| x.odd? } # => [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]   list.reject! { |x| x.odd? } # => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]   # can emulate type of functionality enumerable method? set = [1,5,10] # => [1, 5, 10]  one, five, ten = set # => [1, 5, 10]  1 # => 1  5 # => 5  ten # => 10  # ------------------------------------------------ # method looking ? list = (0..20).to_a odd, = list.select_with_reject { |x| x.odd? } # put matching items first variable # , non-matching second 

sure, can do:

odd, = list.partition &:odd? 

Comments