Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/odds/ext/math.rb,
lib/odds/ext/rand.rb,
lib/odds/ext/map_special.rb

Instance Method Summary collapse

Instance Method Details

#map_centered_on_sum(target_sum) ⇒ Object



2
3
4
5
# File 'lib/odds/ext/map_special.rb', line 2

def map_centered_on_sum(target_sum)
  self_sum = sum
  map { |x| x.to_f / self_sum.to_f * target_sum.to_f }
end

#map_restrict_to_range(r) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/odds/ext/map_special.rb', line 6

def map_restrict_to_range(r)
  map do |n|
    if n > r.end
      r.end
    elsif n < r.begin
      r.begin
    else
      n
    end
  end
end

#rand_elObject



23
24
25
26
# File 'lib/odds/ext/rand.rb', line 23

def rand_el
  i = (rand()*size.to_f).to_i
  self[i]
end

#times_productObject



2
3
4
# File 'lib/odds/ext/math.rb', line 2

def times_product
  inject { |s,i| s * i }
end