Class: VSphereCloud::Resources::Util
- Inherits:
-
Object
- Object
- VSphereCloud::Resources::Util
- Defined in:
- lib/cloud/vsphere/resources/util.rb
Overview
Resources common utility class.
Class Method Summary collapse
-
.average_csv(csv) ⇒ Numeric
Returns the average value from a given CSV string.
-
.weighted_random(list) ⇒ Object
Returns a random item from the given list distributed based on the provided weight.
Class Method Details
.average_csv(csv) ⇒ Numeric
Returns the average value from a given CSV string.
14 15 16 17 18 19 20 |
# File 'lib/cloud/vsphere/resources/util.rb', line 14 def average_csv(csv) values = csv.split(",") result = 0 return result if values.empty? values.each { |v| result += v.to_f } result / values.size end |
.weighted_random(list) ⇒ Object
Returns a random item from the given list distributed based on the provided weight.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cloud/vsphere/resources/util.rb', line 27 def weighted_random(list) return nil if list.empty? weight_sum = list.inject(0) { |sum, x| sum + x[1] } index = rand(weight_sum) offset = 0 list.each do |el, weight| offset += weight return el if index < offset end # Should never happen raise ArgumentError, "index: #{index} sum: #{weight_sum}" end |