0012 Split

A special case to be aware of

The split method presents a particular use case when we need to cut on spaces. Explicitly specifying " " as an argument will not produce the expected result 🤯. The solution is to use a regex / /.

0012-split_1.png

We can trace the history back to the beginning of Ruby on git in 1996 with this comment on this line (and discover at the same time that he took a tour on SVN).

0012-split_2.png

We have selected 2 other interesting uses. It can take a block as an argument, allowing you to iterate directly on the result, thus avoiding an intermediate array.

0012-split_3.png

The cutting pattern can be maintained with groups.

0012-split_4.png

Link to Ruby documentation for split.