Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kw/object regexp #13

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Ruby 3.2+: Add back `Object#=~`

## [1.3.0] - 2024-04-16

- Ruby 3.2+: Add back third argument to Regexp.new, to allow passing "n" to set `Regexp::NOENCODING`.
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ require 'ruby3_backward_compatibility/compatibility/i18n'

The methods `Object#taint` and `Object#untaint` were no-ops for a while but started to raise deprecation warnings.

To add them back as no-ops, use
The method `Object#=~` always returned `nil` if called on anything else than a String. It was removed in Ruby 3.2.

To add back `Object#taint` and `Object#untaint` as no-ops and `Object#=~` on arbitrary receivers, use

```
require 'ruby3_backward_compatibility/compatibility/object'
Expand Down
8 changes: 8 additions & 0 deletions lib/ruby3_backward_compatibility/compatibility/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def taint
def untaint
self
end

# Make the match operator work on arbitrary receivers again.
def =~(regexp)
if Warning[:deprecated]
Warning.warn("deprecated Object#=~ is called on #{self.class}; it always returns nil")
end
nil
end
end
end

Expand Down
32 changes: 32 additions & 0 deletions spec/ruby3_backward_compatibility/compatibility/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,37 @@ module Ruby3BackwardCompatibility
expect(object.untaint).to eq(object)
end
end

describe '#=~' do
it 'returns nil if called on an arbitrary Object' do
object = Object.new
expect(object =~ /asdf/).to be_nil
end

it 'outputs a deprecation warning if called on an arbitrary Object' do
begin
Warning[:deprecated] = true
hash = {}
expect { hash =~ /asdf/ }.to output(/deprecated.*Hash/).to_stderr
ensure
Warning[:deprecated] = false
end
end

it 'still works on String' do
string = '-- asdf --'
expect(string =~ /asdf/).to eq(3)
end

it "doesn't output a deprecation warning if called on a String" do
begin
Warning[:deprecated] = true
string = '-- asdf --'
expect { string =~ /asdf/ }.not_to output(/deprecated/).to_stderr
ensure
Warning[:deprecated] = false
end
end
end
end
end
Loading