YouPlot/lib/youplot/backends/processing.rb
kojix2 c573d690bc Revert "Set the Gem Ruby version to 2.6 or higher"
I changed my mind, it better work on 2.4!

This reverts commit 2649959745.
2021-06-03 10:53:12 +09:00

37 lines
827 B
Ruby

# frozen_string_literal: true
module YouPlot
# plotting functions.
module Backends
module Processing
module_function
def count_values(arr, tally: true, reverse: false)
# tally was added in Ruby 2.7
result = \
if tally && Enumerable.method_defined?(:tally)
arr.tally
else
# value_counts Enumerable::Statistics
arr.value_counts(dropna: false)
end
# sorting
result = result.sort do |a, b|
# compare values
r = b[1] <=> a[1]
# If the values are the same, compare by name
r = a[0] <=> b[0] if r == 0
r
end
# --reverse option
result.reverse! if reverse
# prepare for barplot
result.transpose
end
end
end
end