From b3f980f59860fb3464c11eb0257047206eb16b0d Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Mon, 2 Nov 2020 10:42:04 +0100 Subject: [PATCH] Issue with v2v bin operation and indexes (#501) - added a utility method to fall back to string-sort in case sort raise error - this metod called in v2v_binary method - impending test removed and passes --- lib/daru/helpers/array.rb | 13 +++++++++++++ lib/daru/maths/arithmetic/vector.rb | 2 +- spec/maths/arithmetic/vector_spec.rb | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/daru/helpers/array.rb b/lib/daru/helpers/array.rb index f9cbefe1f..8fa1b3888 100644 --- a/lib/daru/helpers/array.rb +++ b/lib/daru/helpers/array.rb @@ -36,5 +36,18 @@ def array_of?(array, match) !array.empty? && array.all? { |el| match === el } # rubocop:disable Style/CaseEquality end + + def sort_composite_data(array) + array.sort + rescue ArgumentError, TypeError => msg + case msg.to_s + when /comparison of Symbol with String failed/, + /comparison of Symbol with \d+ failed/, + /comparison of String with :.* failed/, + /comparison of Integer with :.* failed/, + /no implicit conversion from nil to integer/ + return array.sort_by(&:to_s) + end + end end end diff --git a/lib/daru/maths/arithmetic/vector.rb b/lib/daru/maths/arithmetic/vector.rb index e914903e1..5777b72ea 100644 --- a/lib/daru/maths/arithmetic/vector.rb +++ b/lib/daru/maths/arithmetic/vector.rb @@ -92,7 +92,7 @@ def v2o_binary operation, other def v2v_binary operation, other, opts={} # FIXME: why the sorting?.. - zverok, 2016-05-18 - index = (@index.to_a | other.index.to_a).sort + index = ArrayHelper.sort_composite_data(@index.to_a | other.index.to_a) elements = index.map do |idx| this = self.index.include?(idx) ? self[idx] : nil diff --git a/spec/maths/arithmetic/vector_spec.rb b/spec/maths/arithmetic/vector_spec.rb index 2d3289db6..cdea53e29 100644 --- a/spec/maths/arithmetic/vector_spec.rb +++ b/spec/maths/arithmetic/vector_spec.rb @@ -24,7 +24,7 @@ end it "appropriately adds vectors with numeric and non-numeric indexes" do - pending "Need an alternate index implementation?" + # pending "Need an alternate index implementation?" v1 = Daru::Vector.new([1,2,3]) v2 = Daru::Vector.new([1,2,3], index: [:a,:b,:c])