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
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.)
Object#=~ was intended for general pattern matching, but presently the role was taken over by
So. How about removing
- 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
Updated by sawa (Tsuyoshi Sawada) over 2 years ago
I agree. At the same time, the
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.
should be changed to raise a
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 mame (Yusuke Endoh) over 2 years 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