Skip to content

Conversation

@BurdetteLamar
Copy link
Member

No description provided.

@BurdetteLamar BurdetteLamar added the Documentation Improvements to documentation. label May 13, 2025
string.c Outdated
Comment on lines 4567 to 4568
* If +object+ is not a string but responds to method <tt>to_str</tt>,
* the comparison is <tt>self == object.to_str</tt>.
Copy link
Member

Choose a reason for hiding this comment

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

The behaviour is confusing, but this is not correct. If object responds to to_str, it DOES NOT call it, but rather calls object == self. If object does not respond to to_str, it just returns false.

For example:

class HasToStr
  def to_str
    raise
  end

  def ==(other)
    puts "HasToStr: comparing to #{other}"
    true
  end
end

class DoesNotHaveToStr
  def ==(other)
    puts "DoesNotHaveToStr: comparing to #{other}"
    true
  end
end

puts "hello" == HasToStr.new
# => HasToStr: comparing to hello
# => true
puts "hello" == DoesNotHaveToStr.new
# => false

Copy link
Member Author

Choose a reason for hiding this comment

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

Recast.

@peterzhu2118 peterzhu2118 merged commit 10e8119 into ruby:master May 14, 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