struct

Scroll::HumanKey

Inherits Comparable < Struct < Value < Object

A --sort comparison key with a defined total order over numeric and non-numeric lines. Lexical keys are represented as non-numeric keys (raw string comparison), so a single concrete type covers both --sort and --human. Under --human, non-numeric lines sort before numeric ones (ascending), lexically among themselves; numeric lines sort by value.

Constants

BASE = 1024.0

Base for byte-suffix magnitudes (GNU sort -h uses 1024).

EXPONENTS = {'b' => 0, 'k' => 1, 'm' => 2, 'g' => 3, 't' => 4}

Unit letter (lowercased) => exponent applied to BASE.

FLOAT = /(?<![\w.])-?\d+\.\d+/

A leading - is a sign only at a boundary; (?<![\w.]) keeps an embedded - (as in a token like a-5k) a separator, so the digits parse positive while a standalone -5 / -1.5 still parses negative.

HUMAN = /(?<!-)(\d+(?:\.\d+)?)([Bb]|[kKmMgGtT][Bb]?)(?=[^\w\d]|$)/

Byte-suffix value: non-negative magnitude + unit letter (+ optional B). The (?<!-) lookbehind enforces that byte values are never negative. The trailing delimiter is a lookahead so it is not consumed into the match.

INT = /(?<![\w.])-?\d+/

Constructors

lexical(source : String) : HumanKey

A non-numeric (lexical) key over the raw string.

Source
new(numeric : Bool, value : Float64, raw : String)
Source
parse(source : String) : HumanKey

Parse one key-source string into a HumanKey. Tries the three numeric patterns and takes the leftmost match; ties in position break by precedence human > float > int (so 1.5k parses as bytes, not 1.5).

Source

Instance methods

<=>(other : HumanKey) : Int32

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Source
numeric?
Source
value
Source