Class: Plotty::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/plotty/graph.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Graph

Returns a new instance of Graph.



88
89
90
91
# File 'lib/plotty/graph.rb', line 88

def initialize(x, y)
  @x = x
  @y = y
end

Class Method Details

.parse(x, y, command) ⇒ Object



93
94
95
96
97
98
# File 'lib/plotty/graph.rb', line 93

def self.parse(x, y, command)
  self.new(
    Sequence.parse(x),
    Function.new(y, command.first),
  )
end

Instance Method Details

#plot!Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/plotty/graph.rb', line 104

def plot!
  r, w = IO.pipe
  
  pid = Process.spawn({'GNUTERM' => 'dumb'}, "gnuplot -p -e \"set terminal dumb #{size.join(' ')}; plot '<cat' with lines notitle\"", in: r, out: STDOUT, err: STDERR)
  
  r.close
  
  @x.each do |x|
    y = @y.call(x)
    w.puts "#{x} #{y}"
  end
  
  w.close
  
  Process.waitpid pid
end

#sizeObject



100
101
102
# File 'lib/plotty/graph.rb', line 100

def size
  TTY::Screen.size.reverse
end