Issue #12786 has been updated by Ryan Ringler.


Some thoughts on method naming...

* New method: `#casecmp?`
  * **PROS:** Nice symmetry with `Fixnum`'s `#<=>` & `#equal?` methods. 'casecmp' seems to imply case-insensitivity (see `String#casecmp` or C's `strcasecmp`)
  * **CONS:** `String#casecmp` returns 0 for case-insensitive matches, which while not falsey, seems less truthy than 1 or -1.

```ruby
# Proposed implementation
> 'abc'.casecmp?('abc') # true
> 'abc'.casecmp?('ABC') # false
> 'abc'.casecmp?('DEF') # false
> 'abc'.casecmp?(:abc)  # TypeError
```

* New method: `#case_equal?`, `#case_insensitive_equal?`, `#insensitive_equal?`, `#iequal?`
  * **PROS:** More expressive method name.
  * **CONS:** New method to learn. Method names without some flavor of 'insensitive' may be unintuitive.

```ruby
# Proposed implementation
> 'abc'.case_equal?('abc') # true
> 'abc'.case_equal?('ABC') # false
> 'abc'.case_equal?('DEF') # false
> 'abc'.case_equal?(:abc)  # false
```

* Repurpose `#eql?`. Per the string.c method description: "Two strings are equal if they have the same length and content". Is 'content' case-insensitive?
  * **PROS:** Seems to align with the description of the method. `#eql?` is currently redundant with `#==`.
  * **CONS:** New context to learn. Backwards incompatible.

```ruby
# Current implementation (Using ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15])
> 'abc'.eql?('abc') # true
> 'abc'.eql?('ABC') # false
> 'abc'.eql?('DEF') # false
> 'abc'.eql?(:abc)  # false

> 'abc' == 'abc'    # true
> 'abc' == 'ABC'    # false
> 'abc' == 'DEF'    # false
> 'abc' == :abc     # false

# Proposed implemntation
> 'abc'.eql?('abc') # true
> 'abc'.eql?('ABC') # true
> 'abc'.eql?('DEF') # false
> 'abc'.eql?(:abc)  # false
```

I currently like `#casecmp?` or `#eql?` but am willing to be convinced otherwise.

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

* Author: Ryan Ringler
* Status: Open
* 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>