Module: Hlockey::Utils

Defined in:
lib/hlockey/utils.rb

Overview

Utilities to help with various Hlockey related tasks

Defined Under Namespace

Classes: Rng

Class Method Summary collapse

Class Method Details

.hash_display_keys(hash) ⇒ Hash<String => Object>

Transforms hash keys to be better for displaying the information in the hash

Parameters:

  • hash (Hash<#to_s => Object>)

Returns:

  • (Hash<String => Object>)

    the new hash



41
42
# File 'lib/hlockey/utils.rb', line 41

def self.hash_display_keys(hash) =
hash.transform_keys { |key| key.to_s.capitalize.gsub("_", " ") }.compact

.weighted_random(hash, prng) ⇒ Object

Does a weighted random with a hash, where the keys are elements being randomly selected and the values are the weights

Parameters:

  • hash (Hash<Object => Integer>)
  • prng (#rand)

Returns:

  • (Object)


49
50
51
52
53
# File 'lib/hlockey/utils.rb', line 49

def self.weighted_random(hash, prng)
  cumulative_weights = []
  hash.each_value { |v| cumulative_weights << v + (cumulative_weights.last or 0) }
  hash.keys[cumulative_weights.index { |n| prng.rand(cumulative_weights.last) < n }]
end