lib/ryba.rb in be9-ryba-0.0.2 vs lib/ryba.rb in be9-ryba-0.0.3

- old
+ new

@@ -7,12 +7,33 @@ case variants when Array variants[rand(variants.size)] when String variants[rand(variants.length),1] + when Range + if variants.exclude_end? + rand(variants.last - variants.first) + variants.first + else + rand((variants.last+1) - variants.first) + variants.first + end end end + + def self.weighted_pick(variants) + total_sum = variants.inject(0) { |sum, var| sum + var[1] } + rnd = rand(total_sum) + + sum = 0 + variants.each do |var| + sum += var[1] + return var[0] if rnd < sum + end + + # этого не может быть + raise + end end require File.join(File.dirname(__FILE__), 'ryba', 'data') require File.join(File.dirname(__FILE__), 'ryba', 'name') require File.join(File.dirname(__FILE__), 'ryba', 'phone_number') +require File.join(File.dirname(__FILE__), 'ryba', 'address')