Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created August 12, 2011 09:49
Show Gist options
  • Save pasberth/1141788 to your computer and use it in GitHub Desktop.
Save pasberth/1141788 to your computer and use it in GitHub Desktop.
URL を置換するだけです
# -*- coding: utf-8 -*-
class String
AVAILABLE_CHARS_IN_URI = [
'\;', '\/', '\?', '\:', '\@', '\&', '\=', '\+', '\$', '\,',
'\-', '\_', '\.', '\!', '\~', '\*', '\\\'', '\(', '\)',
'\%', '0-1', 'a-z', 'A-Z' '\#'
]
REGEXP_OF_URL = /https?\:\/\/[#{AVAILABLE_CHARS_IN_URI.join('')}]+/
def replace_urls &blk
gsub(REGEXP_OF_URL, &blk)
end
end
require "test/unit"
class TestReplaceURLs < Test::Unit::TestCase
def setup
@text = "abcd efg http://example.com hijk lmn https://example.com/index.html opqr stu vwxyz"
@replaced = "abcd efg [[http://example.com]] hijk lmn [[https://example.com/index.html]] opqr stu vwxyz"
end
def test_replace_urls
assert_equal(@replaced, @text.replace_urls { |url| "[[#{url}]]" })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment