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
This commit is contained in:
mame 2018-11-26 07:55:07 +00:00
Родитель f7e94df0d6
Коммит ebff9dc10e
3 изменённых файлов: 28 добавлений и 1 удалений

10
NEWS
Просмотреть файл

@ -217,6 +217,12 @@ sufficient information, see the ChangeLog file or Redmine
* NameError.new accepts +:receiver+ option to set receiver in Ruby * NameError.new accepts +:receiver+ option to set receiver in Ruby
code. [Feature #14313] code. [Feature #14313]
[NilClass]
[New methods]
* NilClass#=~ is added for compatibility. [Feature #15231]
[NoMethodError] [NoMethodError]
[New options] [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 File.readlines do not invoke external commands even if the path starts
with the pipe character <code>'|'</code>. [Feature #14245] with the pipe character <code>'|'</code>. [Feature #14245]
[Object]
* Object#=~ is deprecated. [Feature #15231]
=== Stdlib compatibility issues (excluding feature bug fixes) === Stdlib compatibility issues (excluding feature bug fixes)
* These standard libraries have been promoted to default gems. * These standard libraries have been promoted to default gems.

Просмотреть файл

@ -1479,6 +1479,19 @@ nil_inspect(VALUE obj)
return rb_usascii_str_new2("nil"); 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 * Document-class: TrueClass
* *
@ -1673,6 +1686,7 @@ rb_false(VALUE obj)
static VALUE static VALUE
rb_obj_match(VALUE obj1, VALUE obj2) rb_obj_match(VALUE obj1, VALUE obj2)
{ {
rb_warning("Object#=~ is deprecated; it always returns nil");
return Qnil; 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_a", nil_to_a, 0);
rb_define_method(rb_cNilClass, "to_h", nil_to_h, 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, "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_and, 1);
rb_define_method(rb_cNilClass, "|", false_or, 1); rb_define_method(rb_cNilClass, "|", false_or, 1);
rb_define_method(rb_cNilClass, "^", false_xor, 1); rb_define_method(rb_cNilClass, "^", false_xor, 1);

Просмотреть файл

@ -1528,7 +1528,9 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
def test_refute_match_matcher_object def test_refute_match_matcher_object
@assertion_count = 2 @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 end
def test_refute_match_object_triggered def test_refute_match_object_triggered