Skip to content

Commit

Permalink
all specs passing
Browse files Browse the repository at this point in the history
  • Loading branch information
jtprince committed Aug 27, 2013
1 parent 3c18391 commit d9050ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
27 changes: 14 additions & 13 deletions lib/histogram.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

begin
require 'narray'
rescue LoadError
class NArray
end
class NArray
end

module Histogram

# returns (min, max)
def self.min_max(obj)
mn = obj[0]
mx = obj[0]
obj.each do |val|
if val < mn then mn = val end
if val > mx then mx = val end
def self.minmax(obj)
if obj.is_a?(Array)
obj.minmax
else
mn = obj[0]
mx = obj[0]
obj.each do |val|
if val < mn then mn = val end
if val > mx then mx = val end
end
[mn, mx]
end
[mn, mx]
end

# returns (mean, standard_dev)
Expand Down Expand Up @@ -195,7 +195,8 @@ def histogram(*args)
if opts[:bin_width] || !bins_array_like
calc_min, calc_max =
unless opts[:min] && opts[:max]
(mins, maxs) = all.map(&:minmax).transpose
(mins, maxs) = all.map {|ar| Histogram.minmax(ar) }.transpose
end
[mins.min, maxs.max]
end
_min = opts[:min] || calc_min
Expand Down
27 changes: 15 additions & 12 deletions spec/histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

RSpec::Matchers.define :be_within_rounding_error_of do |expected|
match do |actual|
(act, exp) = [actual, expected].map {|ar| ar.map {|v| v.to_f.round(8) } }
act.should == exp
(act, exp) = [actual, expected].map {|ar| ar.collect {|v| v.to_f.round(8) } }
act.to_a.should == exp.to_a
end
end

Expand Down Expand Up @@ -107,17 +107,20 @@
it_behaves_like 'something that can histogram'
end

begin
describe NArray do
data.each do |obj, ar|
let(obj) { NArray.to_na(ar).to_f.extend(Histogram) }
end
it_behaves_like 'something that can histogram'
have_narray =
begin
require 'narray'
NArray.respond_to?(:to_na)
true
rescue
false
end

describe NArray, :pending => !have_narray do
data.each do |obj, ar|
let(obj) { NArray.to_na(ar).to_f.extend(Histogram) }
end
rescue
puts ""
puts "YOU NEED NArray installed to run NArray tests!"
puts ""
it_behaves_like 'something that can histogram'
end

describe 'calculating bins' do
Expand Down

0 comments on commit d9050ac

Please sign in to comment.