Class: Plotty::Function

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

Instance Method Summary collapse

Constructor Details

#initialize(pattern, command) ⇒ Function

Returns a new instance of Function.



59
60
61
62
# File 'lib/plotty/graph.rb', line 59

def initialize(pattern, command)
  @pattern = Regexp.new(pattern)
  @command = command
end

Instance Method Details

#call(value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/plotty/graph.rb', line 64

def call(value)
  r, w = IO.pipe
  
  puts "Running #{@command} with x = #{value}..."
  
  pid = Process.spawn({'x' => value.to_s}, @command, out: w, err: STDERR)
  
  w.close
  
  buffer = r.read
  
  Process.waitpid pid
  
  if match = @pattern.match(buffer)
    result = match[1] || match[0]
    
    puts "\tresult = #{result}"
    
    return result
  end
end