Class: Plotty::Function

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



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

def command
  @command
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



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

def pattern
  @pattern
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



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

def title
  @title
end

Class Method Details

.parse(pattern, command) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/plotty/graph.rb', line 61

def self.parse(pattern, command)
  pattern = Regexp.new(pattern, Regexp::MULTILINE)
  
  if command =~ /^(\w+):(.*?)$/
    self.new(pattern, $2, $1)
  else
    self.new(pattern, command, command)
  end
end

Instance Method Details

#call(value) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/plotty/graph.rb', line 71

def call(value)
  r, w = IO.pipe
  
  $stderr.puts "Running #{self.command} with x = #{value}..."
  
  pid = Process.spawn({'x' => value.to_s}, self.command, out: w, err: STDERR)
  
  w.close
  
  buffer = r.read
  
  Process.waitpid pid
  
  if match = self.pattern.match(buffer)
    $stderr.puts "\tresult = #{match.inspect}"
    
    if match.captures.empty?
      return [match[0]]
    else
      return match.captures
    end
  end
end