Sha256: a9bba31ecdf9a34cf44d0e3d32a5fd2dbea88a0198b3d123c3070b981fe14755

Contents?: true

Size: 993 Bytes

Versions: 2

Compression:

Stored size: 993 Bytes

Contents

module Ryba
  def self.numerify(number_string)
    number_string.gsub('#') { rand(10).to_s }
  end

  def self.pick(variants)
    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')

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
be9-ryba-0.0.3 lib/ryba.rb
ryba-0.1.0 lib/ryba.rb