Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DOC] Tweaks for String#=~ #13325

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

Merged
merged 2 commits into from
May 15, 2025
Merged

Conversation

BurdetteLamar
Copy link
Member

I could not find any non-Regexp +object+ that did not raise TypeError; are there any?

  • If so, some correction is needed.
  • If not, should we say "Raises TypeError if +object+ is not a Regexp."?

@BurdetteLamar BurdetteLamar added the Documentation Improvements to documentation. label May 13, 2025
Comment on lines -5334 to -5335
* If the given +object+ is not a Regexp, returns the value
* returned by <tt>object =~ self</tt>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We lost this documentation for the case when regexp is not a regexp.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my recent comment below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just create your own object or use any non-string object that responds to #=~ (e.g. nil):

class Foo
  def =~(other)
    puts "calling =~ with #{other}"

    123
  end
end

p "hello" =~ Foo.new
# => calling =~ with hello
# => 123
p "hello" =~ nil
# => nil

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@BurdetteLamar
Copy link
Member Author

Noted in the OP:

I could not find any non-Regexp +object+ that did not raise TypeError; are there any?

* If so, some correction is needed.

* If not, should we say "Raises TypeError if +object+ is not a Regexp."?

@peterzhu2118 peterzhu2118 merged commit 4fc5047 into ruby:master May 15, 2025
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Improvements to documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants