Skip to content

Instantly share code, notes, and snippets.

@bmarini
Created March 3, 2013 20:47
Show Gist options
  • Save bmarini/5078215 to your computer and use it in GitHub Desktop.
Save bmarini/5078215 to your computer and use it in GitHub Desktop.
Multi Split == Multi Recursion
module MultiSplit
def msplit(*delimiters)
return [ self ] if delimiters.empty?
if idx = index( delimiters.first )
[ self[ 0...idx ] ] + self[ ( idx + delimiters.first.length )..-1 ].msplit( *delimiters )
else
msplit( *delimiters[1..-1] )
end
end
end
if $0 == __FILE__
require "test/unit"
String.send :include, MultiSplit
class TestMultiSplit < Test::Unit::TestCase
def test_msplit
result = "abcdefghijklmnopqrstuvwxyz".msplit('a', 'c', 'mno', 'z')
assert_equal [ '', 'b', 'defghijkl', 'pqrstuvwxy', '' ], result
result = "abcdefabcef".msplit('b', 'e')
assert_equal [ 'a', 'cdefa', 'c', 'f' ], result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment