Sha256: a07bee55db3edd7f51c60cf5668f46828bc8ddf6d4c3f66183cebfb7160c199a

Contents?: true

Size: 1017 Bytes

Versions: 14

Compression:

Stored size: 1017 Bytes

Contents

class Hash

  def fast_sort_keys
    keys = self.keys.sort
    Hash[keys.zip(self.values_at(*keys))]
  end

  # TODO write fast sort values

  def populate_with_keys(min: nil, max: nil, explicit_default_value: 0)
    start_key = min || self.keys.min
    end_key = max || self.keys.max

    result = self.dup
    (start_key..end_key).each do |k|
      result[k] = self[k] || explicit_default_value
    end

    return result
  end

  # def collapse_values_on_modulo(integer, min: nil, max: nil, shift: nil)
  #   raise ArgumentError.new("Modulo integer had better be larger than shift") if integer <= shift.to_i
  #
  #   result = Hash.new(0)
  #   hash = self.dup.populate_with_keys(min: min, max: max)
  #   hash = hash.reject{|k, v| k < min.to_i}
  #   hash = hash.reject{|k, v| k > max if max.present?}
  #
  #   hash.each do |k, v|
  #     if k%integer == shift.to_i
  #       result[k] = (k..(k + integer - 1)).map{|i| hash[i].to_i }.sum
  #     end
  #   end
  #
  #   return result.fast_sort_keys
  # end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
creative_rails_utilities-0.4.5 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.4.4 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.4.3 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.4.2 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.4.1 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.4.0 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.3.5 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.3.4 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.3.3 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.3.2 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.3.1 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.3.0 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.2.2 lib/creative_rails_utilities/hash.rb
creative_rails_utilities-0.1.2 lib/creative_rails_utilities/hash.rb