aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2016-07-21 23:41:03 +0200
committerXavier Noria <fxn@hashref.com>2016-07-22 00:15:52 +0200
commit575dbeeefcaafeb566afc07cdd8b55603b698d9f (patch)
tree8371e5e85e11f242c0fbb6fb42fb63b5edac6fa4 /activesupport
parentd4ea18a8cb84601509ee4c6dc691b212af8c2c36 (diff)
downloadrails-575dbeeefcaafeb566afc07cdd8b55603b698d9f.tar.gz
rails-575dbeeefcaafeb566afc07cdd8b55603b698d9f.tar.bz2
rails-575dbeeefcaafeb566afc07cdd8b55603b698d9f.zip
define Range#match? if Ruby < 2.4
See the rationale in the documentation included in this patch. We are going to gradually introduce this predicate in the code base.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md10
-rw-r--r--activesupport/lib/active_support/core_ext/regexp.rb4
-rw-r--r--activesupport/test/core_ext/regexp_ext_test.rb24
3 files changed, 36 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 8d47f99b22..b24f9e6987 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Defines `Regexp.match?` for Ruby versions prior to 2.4. The predicate
+ has the same interface, but it does not have the performance boost. It's
+ purpose is to be able to write 2.4 compatible code.
+
+ *Xavier Noria*
+
* Allow MessageEncryptor to take advantage of authenticated encryption modes.
AEAD modes like `aes-256-gcm` provide both confidentiality and data
@@ -55,7 +61,7 @@
*John Gesimondo*
-* `travel/travel_to` travel time helpers, now raise on nested calls,
+* `travel/travel_to` travel time helpers, now raise on nested calls,
as this can lead to confusing time stubbing.
Instead of:
@@ -69,7 +75,7 @@
preferred way to achieve above is:
- travel 2.days do
+ travel 2.days do
# 2 days from today
end
diff --git a/activesupport/lib/active_support/core_ext/regexp.rb b/activesupport/lib/active_support/core_ext/regexp.rb
index 784145f5fb..d9cff52050 100644
--- a/activesupport/lib/active_support/core_ext/regexp.rb
+++ b/activesupport/lib/active_support/core_ext/regexp.rb
@@ -2,4 +2,8 @@ class Regexp #:nodoc:
def multiline?
options & MULTILINE == MULTILINE
end
+
+ def match?(string, pos=0)
+ !! match(string, pos)
+ end unless //.respond_to?(:match?)
end
diff --git a/activesupport/test/core_ext/regexp_ext_test.rb b/activesupport/test/core_ext/regexp_ext_test.rb
index c2398d31bd..d91e363085 100644
--- a/activesupport/test/core_ext/regexp_ext_test.rb
+++ b/activesupport/test/core_ext/regexp_ext_test.rb
@@ -7,4 +7,28 @@ class RegexpExtAccessTests < ActiveSupport::TestCase
assert_equal false, //.multiline?
assert_equal false, /(?m:)/.multiline?
end
+
+ # Based on https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb.
+ def test_match_p
+ /back(...)/ =~ 'backref'
+ # must match here, but not in a separate method, e.g., assert_send,
+ # to check if $~ is affected or not.
+ assert_equal false, //.match?(nil)
+ assert_equal true, //.match?("")
+ assert_equal true, /.../.match?(:abc)
+ assert_raise(TypeError) { /.../.match?(Object.new) }
+ assert_equal true, /b/.match?('abc')
+ assert_equal true, /b/.match?('abc', 1)
+ assert_equal true, /../.match?('abc', 1)
+ assert_equal true, /../.match?('abc', -2)
+ assert_equal false, /../.match?("abc", -4)
+ assert_equal false, /../.match?("abc", 4)
+ assert_equal true, /\z/.match?("")
+ assert_equal true, /\z/.match?("abc")
+ assert_equal true, /R.../.match?("Ruby")
+ assert_equal false, /R.../.match?("Ruby", 1)
+ assert_equal false, /P.../.match?("Ruby")
+ assert_equal 'backref', $&
+ assert_equal 'ref', $1
+ end
end