Class: BlaBla::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/utilities/util.rb

Class Method Summary collapse

Class Method Details

.diff_in_years_from(date, date_b = Date.today) ⇒ Object



4
5
6
7
# File 'lib/utilities/util.rb', line 4

def self.diff_in_years_from(date, date_b=Date.today)  
  years = date_b.year - date.year  
  date_b < Date.civil(date_b.year, date.month, date.day) ? years - 1 : years  
end

.sortear_por_peso(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/utilities/util.rb', line 9

def self.sortear_por_peso params
  total_pesos = params.map{|par| par[:probabilidade]}.reduce{|a, b|a+b}
  raise "Somatório de pesos deve ser igual a 100" if total_pesos != 100
  
  c = 1
  rnd = rand 1..100
  params.each do |par|
    return par[:valor] if Range.new(c, (c += par[:probabilidade])-1).include? rnd
  end
end