lib/fat_table/column.rb in fat_table-0.9.7 vs lib/fat_table/column.rb in fat_table-0.9.8
- old
+ new
@@ -172,13 +172,13 @@
# :category: Attributes
# Yield each item in the Column in the order in which they appear in the
# Column. This makes Columns Enumerable, so all the Enumerable methods are
# available on a Column.
- def each
- if block_given?
- items.each { |itm| yield itm }
+ def each(&block)
+ if block
+ items.each(&block)
self
else
to_enum(:each)
end
end
@@ -199,11 +199,11 @@
# :category: Aggregates
# Return the first non-nil item in the Column, or nil if all items are
# nil. Works with any Column type.
def first
- return nil if items.all?(&:nil?)
+ return if items.all?(&:nil?)
if type == 'String'
items.reject(&:blank?).first
else
items.filter_to_type(type).first
@@ -212,11 +212,11 @@
# :category: Aggregates
# Return the last non-nil item in the Column. Works with any Column type.
def last
- return nil if items.all?(&:nil?)
+ return if items.all?(&:nil?)
if type == 'String'
items.reject(&:blank?).last
else
items.filter_to_type(type).last
@@ -229,11 +229,11 @@
# column if all items are nil. Works with any Column type.
def count
return items.size if items.all?(&:nil?)
if type == 'String'
- items.reject(&:blank?).count.to_d
+ items.count { |i| !i.blank? }
else
items.filter_to_type(type).count.to_d
end
end
@@ -268,11 +268,11 @@
# Return a Range object for the smallest to largest value in the column,
# or nil if all items are nil. Works with numeric, string, and datetime
# Columns.
def range
only_with('range', 'NilClass', 'Numeric', 'String', 'DateTime')
- return nil if items.all?(&:nil?)
+ return if items.all?(&:nil?)
Range.new(min, max)
end
# :category: Aggregates
@@ -302,11 +302,11 @@
only_with('avg', 'DateTime', 'Numeric')
itms = items.filter_to_type(type)
size = itms.size.to_d
if type == 'DateTime'
- avg_jd = itms.map(&:jd).sum / size
+ avg_jd = itms.sum(&:jd) / size
DateTime.jd(avg_jd)
else
itms.sum / size
end
end
@@ -329,10 +329,11 @@
else
items.filter_to_type(type)
end
n = count
return BigDecimal('0.0') if n <= 1
+
mu = Column.new(header: :mu, items: all_items).avg
sq_dev = BigDecimal('0.0')
all_items.each do |itm|
sq_dev += (itm - mu) * (itm - mu)
end
@@ -351,10 +352,11 @@
return 0 if type == 'NilClass' || items.all?(&:nil?)
only_with('var', 'DateTime', 'Numeric')
n = items.filter_to_type(type).size.to_d
return BigDecimal('0.0') if n <= 1
+
var * ((n - 1) / n)
end
# :category: Aggregates
@@ -469,9 +471,10 @@
# checking for type compatibility. Use the header of this Column as the
# header of the new Column.
def +(other)
msg = 'cannot combine columns with different types'
raise UserError, msg unless type == other.type
+
Column.new(header: header, items: items + other.items)
end
private