DEV Community

thushara wijeratna
thushara wijeratna

Posted on

Ruby partition is great!

Ever had a collection and wanted to do one thing for some items and something else for others? We have partition in ruby that lets us split the collection into two collections.

Ex:

arr = [4,9,10,6,8]
evens, odds = arr.partition {|e| e%2 == 0}
evens.each {|e|
  //process e
}
odds.each {|e|
  //process e
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)