Module: Ryba

Defined in:
lib/ryba.rb,
lib/ryba/data.rb,
lib/ryba/name.rb,
lib/ryba/address.rb,
lib/ryba/company.rb,
lib/ryba/version.rb,
lib/ryba/phone_number.rb

Defined Under Namespace

Modules: Data Classes: Address, Company, Name, PhoneNumber

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.numerify(number_string) ⇒ Object



2
3
4
# File 'lib/ryba.rb', line 2

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

.pick(variants) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ryba.rb', line 6

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

.pick_multi(variants, count) ⇒ Object



21
22
23
# File 'lib/ryba.rb', line 21

def self.pick_multi(variants, count)
  variants.shuffle.take(count)
end

.weighted_pick(variants) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ryba.rb', line 25

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