Class: Yast::Ops::HashComparator Deprecated

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
src/ruby/yast/ops.rb

Overview

Deprecated.

hash comparison usually doesn't make sense

Implements ycp compatible comparison of Hash. It uses lexical comparison for keys and elements.

Instance Method Summary collapse

Constructor Details

#initialize(value, localized = false) ⇒ HashComparator

Returns a new instance of HashComparator



459
460
461
462
# File 'src/ruby/yast/ops.rb', line 459

def initialize(value, localized = false)
  @value = value
  @localized = localized
end

Instance Method Details

#<=>(other) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'src/ruby/yast/ops.rb', line 464

def <=>(other)
  comparator = proc do |k1, k2|
    Ops.comparable_object(k1, @localized) <=> k2
  end
  keys = @value.keys.sort(&comparator)
  other_keys = other.keys.sort(&comparator)

  0.upto(keys.size - 1) do |i|
    res = Ops.comparable_object(keys[i], @localized) <=> other_keys[i]
    return res if res != 0

    res = Ops.comparable_object(@value[keys[i]], @localized) <=> other[keys[i]]
    return res if res != 0
  end

  @value.size <=> other.size
end