Project

General

Profile

Actions

Bug #18163

closed

Unclear String documentation of <=> and casecmp methods

Added by pvalena (Pavel Valena) about 3 years ago. Updated almost 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
[ruby-core:105220]

Description

I have a new student learning Ruby, and there are apparent inconsistencies in Ruby documentation.
The problem appears to be in the order of parameters / variables self and other_string.

<=>
https://docs.ruby-lang.org/en/3.0.0/String.html#method-i-3C-3D-3E

Firstly it says:

 string <=> other_string → -1, 0, 1, or nil

  -1 if other_string is smaller.

I'd expect 'B' <=> 'A' to result in -1 since 'B' is smaller. However that's not the case.

irb(main):001:0> 'B' <=> 'A'
=> 1

casecmp
https://docs.ruby-lang.org/en/3.0.0/String.html#method-i-casecmp

Identical issue, but I think more apparent is with casecmp

casecmp(other_str) → -1, 0, 1, or nil

  -1 if other_string is smaller.

From the description, I expect other_str = 'food' in the following example:

'foo'.casecmp('food') # => -1

food (other_string) is however not smaller than foo, so I'd expect the result to be 1.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0