lib/fat_table/column.rb in fat_table-0.2.8 vs lib/fat_table/column.rb in fat_table-0.2.9

- old
+ new

@@ -19,11 +19,11 @@ # A string representing the deduced type of this Column. One of # Column::TYPES. attr_reader :type # An Array of the items of this Column, all of which must be values of the - # Columns type or a nil. This Array contains the value of the item after + # Column's type or a nil. This Array contains the value of the item after # conversion to a native Ruby type, such as TrueClass, Date, DateTime, # Integer, String, etc. Thus, you can perform operations on the items, # perhaps after removing nils with +.items.compact+. attr_reader :items @@ -90,10 +90,11 @@ @raw_header.to_s.as_sym end @type = 'NilClass' msg = "unknown column type '#{type}" raise UserError, msg unless TYPES.include?(@type.to_s) + @items = [] items.each { |i| self << i } end ########################################################################## @@ -101,12 +102,12 @@ ########################################################################## # :category: Attributes # Return the item of the Column at the given index. - def [](k) - items[k] + def [](idx) + items[idx] end # :category: Attributes # Return a dupped Array of this Column's items. To get the non-dupped items, @@ -227,15 +228,17 @@ # numeric and datetime Columns. For datetime Columns, it converts each date # to its Julian day number, computes the average, and then converts the # average back to a DateTime. def avg only_with('avg', 'DateTime', 'Numeric') + itms = items.compact + size = itms.size.to_d if type == 'DateTime' - avg_jd = items.compact.map(&:jd).sum / items.compact.size.to_d + avg_jd = itms.map(&:jd).sum / size DateTime.jd(avg_jd) else - sum / items.compact.size.to_d + itms.sum / size end end # :category: Aggregates @@ -513,19 +516,19 @@ # Convert the val to a Numeric if is already a Numeric or is a String that # looks like one. Any Float is promoted to a BigDecimal. Otherwise return # nil. def convert_to_numeric(val) - return BigDecimal.new(val, Float::DIG) if val.is_a?(Float) + return BigDecimal(val, Float::DIG) if val.is_a?(Float) return val if val.is_a?(Numeric) # Eliminate any commas, $'s (or other currency symbol), or _'s. cursym = Regexp.quote(FatTable.currency_symbol) clean_re = /[,_#{cursym}]/ val = val.to_s.clean.gsub(clean_re, '') return nil if val.blank? case val when /(\A[-+]?\d+\.\d*\z)|(\A[-+]?\d*\.\d+\z)/ - BigDecimal.new(val.to_s.clean) + BigDecimal(val.to_s.clean) when /\A[-+]?[\d]+\z/ val.to_i when %r{\A([-+]?\d+)\s*[:/]\s*([-+]?\d+)\z} Rational($1, $2) end