Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby rpartition vs partition?

Tags:

ruby

What is the difference between rpartition and partition? I have read the documentation but I see them like the same. Is it just that ones came up in a later ruby version?

like image 900
emerak Avatar asked Apr 28 '15 21:04

emerak


Video Answer


1 Answers

The following example will help identify the difference:

"abccba".partition("b")
# => ["a", "b", "ccba"]

"abccba".rpartition("b")
# => ["abcc", "b", "a"]

So the difference is that rpartition searches for the rightmost occurrence, instead of the leftmost one.

like image 131
JuniorCompressor Avatar answered Nov 15 '22 07:11

JuniorCompressor