diff --git a/lib/nyaplot/data.rb b/lib/nyaplot/data.rb index 8c2f5d3..3c317ea 100644 --- a/lib/nyaplot/data.rb +++ b/lib/nyaplot/data.rb @@ -223,6 +223,16 @@ def label @label end + [:+, :-, :/, :*].each do |operator| + define_method(operator) do |obj| + if obj.is_a? Nyaplot::Series + Nyaplot::Series.new(label, (self.zip obj.to_a).map { |arr| arr.inject(&operator) }) + elsif obj.is_a? Numeric + Nyaplot::Series.new(label, self.map { |e| e.send(operator, obj) }) + end + end + end + def method_missing(meth, *args, &block) if @arr.respond_to?(meth) @arr.send(meth, *args, &block) diff --git a/spec/nyaplot/data_spec.rb b/spec/nyaplot/data_spec.rb index 5e8a1c1..102f93f 100644 --- a/spec/nyaplot/data_spec.rb +++ b/spec/nyaplot/data_spec.rb @@ -176,4 +176,60 @@ expect(@series.size).to eq(@series.to_a.size) end end + + context "#+" do + it "should add each element of the two Series" do + another_series = Nyaplot::Series.new('another_series', [20, 40]) + + expect((@series + another_series).to_a).to eq( [30, 70] ) + end + + it "should add the number to each element of the Series" do + number = 10 + + expect((@series + number).to_a).to eq( [20, 40] ) + end + end + + context "#-" do + it "should subtract the passed Series from the Series" do + another_series = Nyaplot::Series.new('another_series', [20, 40]) + + expect((@series - another_series).to_a).to eq( [-10, -10] ) + end + + it "should subtract the number from the Series" do + number = 10 + + expect((@series - number).to_a).to eq( [0, 20] ) + end + end + + context "#/" do + it "should divide each element of the Series by the each element of the passed Series" do + another_series = Nyaplot::Series.new('another_series', [20.to_f, 40.to_f]) + + expect((@series / another_series).to_a).to eq( [10/20.to_f, 30/40.to_f] ) + end + + it "should divide each element of the Series by the passed number" do + number = 10.to_f + + expect((@series / number).to_a).to eq( [10/10.to_f, 30/10.to_f] ) + end + end + + context "#*" do + it "should multiply each element of the two Series together" do + another_series = Nyaplot::Series.new('another_series', [20, 40]) + + expect((@series * another_series).to_a).to eq( [200, 1200] ) + end + + it "should multiply each element of the Series by the number" do + number = 10 + + expect((@series * number).to_a).to eq( [100, 300] ) + end + end end