Here is my code so far… you welcome to rip it.
Comments would be nice 
ruby test_misc.rb
TestMisc#test_pop_until .
TestMisc#test_pop_until_non_existing_class .
TestMisc#test_pop_until_regex .
TestMisc#test_shift_until .
TestMisc#test_shift_until_non_existing_class .
TestMisc#test_shift_until_regex .
Time: 0.009481
OK (6/6 tests 12 asserts)
expand -t4 test_misc.rb
require ‘misc’
require ‘runit/testcase’
require ‘runit/assert’
require ‘runit/cui/testrunner’
#Array.extend ArrayMisc # BOOM, why does this not work ?
class Array; include ArrayMisc; end
class TestMisc < RUNIT::TestCase
def test_shift_until
ary = [1, 2, “a”, 3, 4, “b”, 5, 6]
res = ary.shift_until(String)
assert_equal([1, 2], res)
assert_equal([“a”, 3, 4, “b”, 5, 6], ary)
end
def test_shift_until_non_existing_class
ary = [1, 2, “a”, 3, 4, “b”, 5, 6]
res = ary.shift_until(IO)
assert_equal(, ary)
assert_equal([1, 2, “a”, 3, 4, “b”, 5, 6], res)
end
def test_shift_until_regex
ary = [1, 2, “a”, 3, 4, “b”, 5, 6]
res = ary.shift_until(/^b/)
assert_equal([“b”, 5, 6], ary)
assert_equal([1, 2, “a”, 3, 4], res)
end
def test_pop_until
ary = [1, 2, “a”, 3, 4, “b”, 5, 6]
res = ary.pop_until(String)
assert_equal([5, 6], res)
assert_equal([1, 2, “a”, 3, 4, “b”], ary)
end
def test_pop_until_non_existing_class
ary = [1, 2, “a”, 3, 4, “b”, 5, 6]
res = ary.pop_until(IO)
assert_equal(, ary)
assert_equal([1, 2, “a”, 3, 4, “b”, 5, 6], res)
end
def test_pop_until_regex
ary = [1, 2, “a”, 3, 4, “b”, 5, 6]
res = ary.pop_until(/^a/)
assert_equal([1, 2, “a”], ary)
assert_equal([3, 4, “b”, 5, 6], res)
end
end
if $0 == FILE
RUNIT::CUI::TestRunner.run(TestMisc.suite)
end
expand -t4 misc.rb
module ArrayMisc
def shift_until(klass)
res =
while length != 0 and not klass === first
res << self.shift
end
res
end
def pop_until(klass)
res =
while length != 0 and not klass === last
res.unshift(self.pop)
end
res
end
end
···
–
Simon Strandgaard