Skip to content

Instantly share code, notes, and snippets.

@dixia
Created October 8, 2013 11:51
Show Gist options
  • Save dixia/6883525 to your computer and use it in GitHub Desktop.
Save dixia/6883525 to your computer and use it in GitHub Desktop.
'20131007'.cut 3,5 => ['2013','10','07']
require 'test/unit'
class String
def cut *arg
raise TypeError unless arg.is_a? Array and arg.first.is_a? Fixnum
start = 0
arg.map do |n|
r = start..n
start = n + 1
r
end.push(start..-1).map do |r|
self[r]
end
end
end
class TestString < Test::Unit::TestCase
def test_cut
substrs = '20131007'.cut 3,5
assert_equal ['2013','10','07'],substrs
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment