Skip to content

Instantly share code, notes, and snippets.

@djbender
Forked from JEG2/extensions.rb
Created October 4, 2012 23:31
Show Gist options
  • Save djbender/3837125 to your computer and use it in GitHub Desktop.
Save djbender/3837125 to your computer and use it in GitHub Desktop.
module Extensions
module_function
def separate(path)
extensions = path.reverse
.scan(/\G\w+\./)
.map { |e| e[0..-2].reverse }
.reverse
file_name = path.split(".")[0..-(extensions.size + 1)].join(".")
[file_name, extensions]
end
end
if $PROGRAM_NAME == __FILE__
require "minitest/autorun"
class SeparateExtensions < MiniTest::Unit::TestCase
def test_extensions_are_pulled_off_the_end
assert_equal(
["some file name with", ["dots", "html", "markdown"]],
Extensions.separate("some file name with.dots.html.markdown")
)
end
def test_sections_with_spaces_are_ignored
assert_equal(
["some file name.with dots", ["html", "markdown"]],
Extensions.separate("some file name.with dots.html.markdown")
)
end
def test_treats_the_leading_section_as_part_of_the_file_name
assert_equal(
["some.file name with.multiple dots", ["html", "markdown"]],
Extensions.separate("some.file name with.multiple dots.html.markdown")
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment