Class: Graph::Function::Comparison

Inherits:
Object
  • Object
show all
Includes:
PlotConfig, ReformatString
Defined in:
lib/graph/function/comparison.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PlotConfig

#set_up

Methods included from ReformatString

#camel_title, #escape_underscores

Constructor Details

#initialize(generator) ⇒ Comparison

Returns a new instance of Comparison.



8
9
10
# File 'lib/graph/function/comparison.rb', line 8

def initialize(generator)
  @data_generator = generator
end

Instance Attribute Details

#data_generatorObject

Returns the value of attribute data_generator.



6
7
8
# File 'lib/graph/function/comparison.rb', line 6

def data_generator
  @data_generator
end

Instance Method Details

#of(*methods) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graph/function/comparison.rb', line 12

def of(*methods)
  fail unless methods.all? {|m| m.is_a?(Method) }

  methods.each do |m|
    self.class.send(:define_method, m.name, proc(&m))
  end

  Gnuplot.open do |gp|
    Gnuplot::Plot.new(gp) do |plot|

      plot.title  "#{title = methods.map {|m| camel_title(m.name) }.join(', ') }"
set_up(plot)

      x = Graph::Function.configuration.step
      pb = ProgressBar.create(title: title, total: x.size)

      methods.each do |m|
        pb.reset
        y = x.collect do |v|
          pb.increment
          data = data_generator.call(v)
          # FIXME can i get ride of the cost of `send`?
          Benchmark.measure { self.send(m.name, data) }.real
        end

        plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
          ds.with = "linespoints"
          ds.title = "#{escape_underscores(m.name)}"
        end
      end
    end
  end
end