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.



463
464
465
466
# File 'src/ruby/yast/ops.rb', line 463

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

Instance Method Details

#<=>(other) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'src/ruby/yast/ops.rb', line 468

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