Ruby Partition Methods
One cool way to chop through a Ruby string is the partition
String method. This uses regex to search for a match in a string. If it finds one, it returns what came before the match, the match, and what came after as elements in an array.
2.2.2 :001 > 'foo bar baz bat'.partition 'bar'
=> ["foo ", "bar", " baz bat"]
This is not to be confused with the partition
Array method, which takes a block. Any elements that evaluate true are returned first, followed by the elements that evaluate false.
2.2.2 :002 > [1,2,3,4,5].partition(&:even?)
=> [[2, 4], [1, 3, 5]]
Tweet