Let’s say I have a min and a max number. max can be anything, but min will always be greater than zero.
I can get the range min..max and let’s say I have a third number, count — I want to divide the range by 10 (or some other number) to get a new scale. So, if the range is 1000, it would increment in values of 100, 200, 300, and find out where the count lies within the range, based on my new scale. So, if count is 235, it would return 2 because that’s where it lies on the range scale.
Am I making any sense? I’m trying to create a heat map based on a range of values, basically … so I need to create the scale based on the range and find out where the value I’m testing lies on that new scale.
I was working with something like this, but it didn’t do it:
def heat_map(project, word_count, division)
unless word_count == 0
max = project.words.maximum('quantity')
min = project.words.minimum('quantity')
range = min..max
total = range.count
break_point = total / division
heat_index = total.to_f / word_count.to_f
heat_index.round
else
"freezing"
end
end
I figured there’s probably an easier ruby way I’m missing.
Why not just use arithmetic and rounding? Assuming that
numberis betweenminandmaxand you want the range split inton_divdivisions andxis the number you want to find the index of (according to above it looks likemin = 0,max= 1000,n_div = 10, andx = 235):Then
heat_index(235, 0, 1000, 10)gives 2.