class DataCalc
Attributes
input[RW]
length[RW]
output[RW]
Public Class Methods
new(input)
click to toggle source
# File lib/datacalc.rb, line 6 def initialize(input) @input = JSON.parse(input) @length = @input.length @output = nil end
Public Instance Methods
match(*matchnames)
click to toggle source
What: Gets list entries that include all values of arguments passed Input: Variable number of arrays with field name and value Output: Parsed JSON of entries that match all input arrays
# File lib/datacalc.rb, line 26 def match(*matchnames) savefile = "[]" matchnames.each do |match| tmpfile = Array.new (0..@length-1).each do |l| if match[1] == (@input[l])[match[0]] tmpfile << @input[l] savefile = tmpfile.to_json end end @input = JSON.parse(savefile) @length = @input.length end @output = @input end
mean(col)
click to toggle source
What: Get average of a column Input: Column to get average of Output: Average
# File lib/datacalc.rb, line 59 def mean(col) mean = sum(col).to_f / @length.to_f end
sortalpha(sattr)
click to toggle source
What: Sorts in alphabetical order Input: Attribute to sort by Output: Sorted JSON
# File lib/datacalc.rb, line 52 def sortalpha(sattr) @input.sort_by {|e| e[sattr]}.to_json end
sortnum(sattr)
click to toggle source
What: Sorts in numerical order Input: Attribute to sort by Output: Sorted JSON
# File lib/datacalc.rb, line 45 def sortnum(sattr) @input.sort_by {|e| e[sattr].to_i}.to_json end
sum(col)
click to toggle source
What: Gets the sum of a column Input: Column to add up Output: Sum for column
# File lib/datacalc.rb, line 15 def sum(col) total = 0 (0..@length-1).each do |l| total += (@input[l])[col].to_i end return total end