Project

General

Profile

Feature #15231

Remove `Object#=~`

Added by mame (Yusuke Endoh) 5 months ago. Updated 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:89438]

Description

Object#=~ receives (and just discards) an argument, and always returns nil. What purpose is this method for?

The following behavior that Object#=~ caused was confusing to me.

["foo"] =~ /foo/ #=> nil

More precisely: the actual example that I encountered was to parse coverage data from output of coverage measurement tool by using Open3.capture2:

out = Open3.capture2("gcov", ...) # BUG: `out, =` was intended
if out =~ /lines\.*: *(\d+\.\d+)%/
  ...
end

Obviously, I forgot a comma to receive the return value of Open3.capture2. The method returns a two-element array, and out =~ calls Object#=~, which hid the bug. (Worse, I took several tens of minutes to debug it because I first thought that this is a bug of regexp, and spent tweaking the regexp.)

I guess Object#=~ was intended for general pattern matching, but presently the role was taken over by Object#===.

So. How about removing Object#=~?

Concerns:

  • usa (Usaku NAKAMURA) said NilClass#=~ should be newly introduced because of: if gets =~ /re/
  • Object#!~ is difficult to remove: some classes define only #=~, and expect Object#!~ to delegate to #=~.

Associated revisions

Revision 7a635a7d
Added by mame (Yusuke Endoh) 4 months ago

lib/webrick: explicitly convert header values to a string

The values of @header are expected to be all strings;
WEBrick::HTTPResponse::[]=(key, val) explicitly converts the second
argument to a string and assigns it to @header hash.
However, there were some points in WEBrick internal code that assigns
non-String to @header. This change fixes the issues.

The values are checked by header_value =~ /\r\n/ in check_header.
The type confusion caused conflict with removal of Object#=~
[Feature #15231].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65984
Added by mame (Yusuke Endoh) 4 months ago

lib/webrick: explicitly convert header values to a string

The values of @header are expected to be all strings;
WEBrick::HTTPResponse::[]=(key, val) explicitly converts the second
argument to a string and assigns it to @header hash.
However, there were some points in WEBrick internal code that assigns
non-String to @header. This change fixes the issues.

The values are checked by header_value =~ /\r\n/ in check_header.
The type confusion caused conflict with removal of Object#=~
[Feature #15231].

Revision 65984
Added by mame (Yusuke Endoh) 4 months ago

lib/webrick: explicitly convert header values to a string

The values of @header are expected to be all strings;
WEBrick::HTTPResponse::[]=(key, val) explicitly converts the second
argument to a string and assigns it to @header hash.
However, there were some points in WEBrick internal code that assigns
non-String to @header. This change fixes the issues.

The values are checked by header_value =~ /\r\n/ in check_header.
The type confusion caused conflict with removal of Object#=~
[Feature #15231].

Revision 03cde6c8
Added by mame (Yusuke Endoh) 4 months ago

lib/rdoc/markup/: Remove wrong call to =~ against Array

@res is an Array, so @res =~ /\n\z/ calls Object#=~ which always
returns nil.
I guess it should be @res.last =~ /\n\z/, but the change causes test
failures.

This bug was found during work for removal of Object#=~.
[Feature #15231]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65985
Added by mame (Yusuke Endoh) 4 months ago

lib/rdoc/markup/: Remove wrong call to =~ against Array

@res is an Array, so @res =~ /\n\z/ calls Object#=~ which always
returns nil.
I guess it should be @res.last =~ /\n\z/, but the change causes test
failures.

This bug was found during work for removal of Object#=~.
[Feature #15231]

Revision 65985
Added by mame (Yusuke Endoh) 4 months ago

lib/rdoc/markup/: Remove wrong call to =~ against Array

@res is an Array, so @res =~ /\n\z/ calls Object#=~ which always
returns nil.
I guess it should be @res.last =~ /\n\z/, but the change causes test
failures.

This bug was found during work for removal of Object#=~.
[Feature #15231]

Revision e0dd7b4d
Added by mame (Yusuke Endoh) 4 months ago

test/rdoc/test_rdoc_rdoc.rb: add dummy finish

RDoc::Options#@exclude is initialized as an empty array.
Then, #finish converts it to a regexp or nil and reassign it to @exclude.
Some methods of RDoc assumes that #finish has been already called.
So, this change forces to assign nil to @exclude.

This type confusion was found during work for removal of Object#=~.
[Feature #15231]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65986 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65986
Added by mame (Yusuke Endoh) 4 months ago

test/rdoc/test_rdoc_rdoc.rb: add dummy finish

RDoc::Options#@exclude is initialized as an empty array.
Then, #finish converts it to a regexp or nil and reassign it to @exclude.
Some methods of RDoc assumes that #finish has been already called.
So, this change forces to assign nil to @exclude.

This type confusion was found during work for removal of Object#=~.
[Feature #15231]

Revision 65986
Added by mame (Yusuke Endoh) 4 months ago

test/rdoc/test_rdoc_rdoc.rb: add dummy finish

RDoc::Options#@exclude is initialized as an empty array.
Then, #finish converts it to a regexp or nil and reassign it to @exclude.
Some methods of RDoc assumes that #finish has been already called.
So, this change forces to assign nil to @exclude.

This type confusion was found during work for removal of Object#=~.
[Feature #15231]

Revision f7e94df0
Added by mame (Yusuke Endoh) 4 months ago

lib/rubygems: explicitly clarify the type for =~ matching

RubyGems is very indifferent for type.
This change is needed for removal of Object#=~. [Feature #15231]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65988 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65988
Added by mame (Yusuke Endoh) 4 months ago

lib/rubygems: explicitly clarify the type for =~ matching

RubyGems is very indifferent for type.
This change is needed for removal of Object#=~. [Feature #15231]

Revision 65988
Added by mame (Yusuke Endoh) 4 months ago

lib/rubygems: explicitly clarify the type for =~ matching

RubyGems is very indifferent for type.
This change is needed for removal of Object#=~. [Feature #15231]

Revision ebff9dc1
Added by mame (Yusuke Endoh) 4 months ago

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

Revision 65989
Added by mame (Yusuke Endoh) 4 months ago

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]

Revision 65989
Added by mame (Yusuke Endoh) 4 months ago

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]

Revision 973f84ef
Added by nobu (Nobuyoshi Nakada) 4 months ago

Show the class of the receiver [Feature #15231]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 66261
Added by nobu (Nobuyoshi Nakada) 4 months ago

Show the class of the receiver [Feature #15231]

Revision 66261
Added by nobu (Nobuyoshi Nakada) 4 months ago

Show the class of the receiver [Feature #15231]

History

Updated by naruse (Yui NARUSE) 5 months ago

rubocop should alert obj =~ re to fix as re =~ obj.

#2

Updated by znz (Kazuhiro NISHIYAMA) 5 months ago

  • Description updated (diff)
#3

Updated by nobu (Nobuyoshi Nakada) 5 months ago

  • Description updated (diff)

Updated by sawa (Tsuyoshi Sawada) 5 months ago

I agree. At the same time, the String=~ behaviour:

str =~ obj → integer or nil click to toggle source
Match—If obj is a Regexp, use it as a pattern to match against str,and returns the position the match starts, or nil if there is no match. Otherwise, invokes obj.=~, passing str as an argument. The default =~ in Object returns nil.
(from https://ruby-doc.org/core-2.4.0/String.html#method-i-3D~)

should be changed to raise a TypeError when obj is not a Regexp, on a par with Regexp#=~ given a non-String argument. Afterall, the following unnatural asymmetry in the current behaviour should be resolved:

// =~ Object.new # >> TypeError: no implicit conversion of Object into String
"" =~ Object.new # => nil

Updated by znz (Kazuhiro NISHIYAMA) 5 months ago

"" =~ "" #=> TypeError (type mismatch: String given)

Updated by matz (Yukihiro Matsumoto) 4 months ago

I vote for the proposed change. Let's give deprecation warning first. I don't think we need to remove !~ from Kernel.

Matz.

#7

Updated by mame (Yusuke Endoh) 4 months ago

  • Status changed from Open to Closed

Applied in changeset trunk|r65984.


lib/webrick: explicitly convert header values to a string

The values of @header are expected to be all strings;
WEBrick::HTTPResponse::[]=(key, val) explicitly converts the second
argument to a string and assigns it to @header hash.
However, there were some points in WEBrick internal code that assigns
non-String to @header. This change fixes the issues.

The values are checked by header_value =~ /\r\n/ in check_header.
The type confusion caused conflict with removal of Object#=~
[Feature #15231].

Also available in: Atom PDF