summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-26 07:55:07 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-26 07:55:07 +0000
commitebff9dc10e6e72239c23e50acc7d3cbfdc659e7a (patch)
tree26182851eb992cc67916e9e9ace053e2aaac5cc2
parentf7e94df0d66aff47745d189ab5a7858a92233d57 (diff)
object.c: Deprecate Object#=~ and add NilClass#=~`
Object#=~ always returns nil. This behavior is not only unuseful but also troublesome because it may hide a type error. This change deprecates Object#=~. For compatibility, NilClass#=~ is newly introduced. [Feature #15231] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65989 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--NEWS10
-rw-r--r--object.c15
-rw-r--r--test/minitest/test_minitest_unit.rb4
3 files changed, 28 insertions, 1 deletions
diff --git a/NEWS b/NEWS
index 115eda0811..189916a04a 100644
--- a/NEWS
+++ b/NEWS
@@ -217,6 +217,12 @@ sufficient information, see the ChangeLog file or Redmine
* NameError.new accepts +:receiver+ option to set receiver in Ruby
code. [Feature #14313]
+[NilClass]
+
+ [New methods]
+
+ * NilClass#=~ is added for compatibility. [Feature #15231]
+
[NoMethodError]
[New options]
@@ -447,6 +453,10 @@ sufficient information, see the ChangeLog file or Redmine
File.readlines do not invoke external commands even if the path starts
with the pipe character <code>'|'</code>. [Feature #14245]
+[Object]
+
+ * Object#=~ is deprecated. [Feature #15231]
+
=== Stdlib compatibility issues (excluding feature bug fixes)
* These standard libraries have been promoted to default gems.
diff --git a/object.c b/object.c
index 14efb86434..0160151d3e 100644
--- a/object.c
+++ b/object.c
@@ -1479,6 +1479,19 @@ nil_inspect(VALUE obj)
return rb_usascii_str_new2("nil");
}
+/*
+ * call-seq:
+ * nil =~ other -> nil
+ *
+ * Dummy pattern matching -- always returns nil.
+ */
+
+static VALUE
+nil_match(VALUE obj1, VALUE obj2)
+{
+ return Qnil;
+}
+
/***********************************************************************
* Document-class: TrueClass
*
@@ -1673,6 +1686,7 @@ rb_false(VALUE obj)
static VALUE
rb_obj_match(VALUE obj1, VALUE obj2)
{
+ rb_warning("Object#=~ is deprecated; it always returns nil");
return Qnil;
}
@@ -4149,6 +4163,7 @@ InitVM_Object(void)
rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
+ rb_define_method(rb_cNilClass, "=~", nil_match, 1);
rb_define_method(rb_cNilClass, "&", false_and, 1);
rb_define_method(rb_cNilClass, "|", false_or, 1);
rb_define_method(rb_cNilClass, "^", false_xor, 1);
diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb
index 833d582496..3d92b865c3 100644
--- a/test/minitest/test_minitest_unit.rb
+++ b/test/minitest/test_minitest_unit.rb
@@ -1528,7 +1528,9 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
def test_refute_match_matcher_object
@assertion_count = 2
- @tc.refute_match Object.new, 5 # default #=~ returns false
+ non_verbose do
+ @tc.refute_match Object.new, 5 # default #=~ returns false
+ end
end
def test_refute_match_object_triggered