Class: NumPlot::Process

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

Overview

The class communicating a gnuplot subprocess.

In many cases, you use NumPlot::Plotter#plot and you don't need to use this class directly.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Process) initialize(pipe, wait)

Create a Process object for already opened pipe. Normally, you had better to use open to create a new object.

Parameters:

  • pipe

    a pipe to communicate to a subprocess

  • wait

    wait for the output from a subprocess



162
163
164
165
166
# File 'lib/numplot.rb', line 162

def initialize(pipe, wait)
  @pipe = pipe
  @wait = wait
  @output = nil
end

Instance Attribute Details

- (String?) output (readonly)

Output string from a gnuplot subprocess.

Returns:

  • (String, nil)


170
171
172
# File 'lib/numplot.rb', line 170

def output
  @output
end

Class Method Details

+ (Object) open(persist = true, waiting = true, name = "gnuplot")

Open a gnuplot process.

If a block is given, the block is invoked with the opened Process object. Otherwise, opened Process object is returned. If you give true as persist, -persist option is added to the command line of gnuplot.

Parameters:

  • persist (defaults to: true)

    add -persist option to gnuplot as a command line option

  • waiting (defaults to: true)

    wait for termination of stdout of gnuplot pipe

  • name (defaults to: "gnuplot")

    the name of the gnuplot executable file



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/numplot.rb', line 207

def self.open(persist=true, waiting=true, name="gnuplot")
  pr = new(IO.popen(command(name, persist), "w+"), waiting)
  
  if block_given?
    begin
      yield pr
    ensure
      pr.close
    end
    return pr.output
  else
    return pr
  end
end

Instance Method Details

- (void) close

This method returns an undefined value.

Close the pipe and terminate the subprocess.

If you specify wait=true when the object is constructed,



176
177
178
179
180
181
182
183
184
# File 'lib/numplot.rb', line 176

def close
  if @wait
    @pipe.close_write
    @output = @pipe.read
    @pipe.close
  else
    @pipe.close
  end
end

- (void) puts(str)

This method returns an undefined value.

Write str and a newline to the subprocess



194
195
196
# File 'lib/numplot.rb', line 194

def puts(str)
  @pipe.puts(str)
end

- (void) write(str)

This method returns an undefined value.

Write str to the subprocess.



188
189
190
# File 'lib/numplot.rb', line 188

def write(str)
  @pipe.write(str)
end