Issue #12786 has been updated by Martin Drst.


Ryan Ringler wrote:

> The attached patch implements `#casecmp?` by calling `#casecmp` and checking whether the return value is zero.  I think this provides a clean separation of concerns (keeping all the comparison logic in `#casecmp`.)

Thanks for the patch. "Separation of concerns" is in general a good idea, but in this case, it will lead to a suboptimal result. In particular, as discussed in https://bugs.ruby-lang.org/issues/12786#note-1, we can easily make sure that casecmp? works with all of Unicode, so that e.g.

```
assert_equal(true, ''.casecmp? '')
```

works. But your patch doesn't do this. Making sure that casecmp itself works for all of Unicode is extremely difficult, because it needs full Unicode sorting.

Btw, this reminds me that we need options such as :turkic on casecmp?. The simplest set of tests would be:

```
assert_equal(true, 'I'.casecmp? 'i')
assert_equal(true, 'I'.casecmp? '', :turkic)
assert_equal(true, 'i'.casecmp? '', :turkic)
```

So my implementation above would change to

```
class String
  def casecmp? (other, *args)
    downcase(:fold, *args) == other.downcase(:fold, *args)
  end
end
```

Given that casecmp? can easily be made to work across Unicode soon, whereas for casecmp, this may take another few years or so, I wonder whether it might not be better to use a slightly different name, e.g. case_equal? or so.

----------------------------------------
Feature #12786: String#casecmp?
https://bugs.ruby-lang.org/issues/12786#change-61756

* Author: Ryan Ringler
* Status: Feedback
* Priority: Normal
* Assignee: 
----------------------------------------
Description

I would find String#casecmp? convenience method handy.  I don't believe I've ever called String#casecmp without chaining #zero? to the result.

```ruby
'abc'.casecmp?('ABC') #=> true
'abc'.casecmp?('DEF') #=> false
```

---Files--------------------------------
string_casecmp_.patch (4.97 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>