Module: Gnuplot

Defined in:
lib/gnuplot.rb

Defined Under Namespace

Classes: DataSet, Plot, SPlot

Class Method Summary collapse

Class Method Details

.gnuplot(persist = true) ⇒ Object

Find the path to the gnuplot executable. The name of the executable can be specified using the RB_GNUPLOT environment variable but will default to the command ‘gnuplot’.

persist [bool] Add the persist flag to the gnuplot executable

Return the path to the gnuplot executable or nil if one cannot be found.



57
58
59
60
61
62
63
# File 'lib/gnuplot.rb', line 57

def Gnuplot.gnuplot( persist = true )
  exe_loc = which( ENV['RB_GNUPLOT'] || 'gnuplot' )
  raise 'gnuplot executable not found on path' unless exe_loc
  cmd = '"' + exe_loc + '"'
  cmd += " -persist" if persist
  cmd
end

.open(persist = true) ⇒ Object

Open a gnuplot process that exists in the current PATH. If the persist flag is true then the -persist flag is added to the command line. The path to the gnuplot executable is determined using the ‘which’ command.

See the gnuplot documentation for information on the persist flag.

todo Add a method to pass the gnuplot path to the function.



73
74
75
76
77
78
79
80
81
# File 'lib/gnuplot.rb', line 73

def Gnuplot.open( persist=true )
  cmd = Gnuplot.gnuplot( persist )
  IO::popen( cmd, "w+") { |io|
    yield io
    io.close_write
    @output = io.read
  }
  return @output	
end

.which(bin) ⇒ Object

Trivial implementation of the which command that uses the PATH environment variable to attempt to find the given application. The application must be executable and reside in one of the directories in the PATH environment to be found. The first match that is found will be returned.

bin [String] The name of the executable to search for.

Return the full path to the first match or nil if no match is found.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gnuplot.rb', line 19

def Gnuplot.which ( bin )
  if RUBY_PLATFORM =~ /mswin|mingw/
    all = [bin, bin + '.exe']
  else
    all = [bin]
  end
  for exec in all
    if which_helper(exec)
      return which_helper(exec)
    end
  end
  return nil
end

.which_helper(bin) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gnuplot.rb', line 33

def Gnuplot.which_helper bin
  return bin if File::executable? bin

  path = ENV['PATH']
  path.split(File::PATH_SEPARATOR).each do |dir|
    candidate = File::join dir, bin.strip
    return candidate if File::executable? candidate
  end

  # This is an implementation that works when the which command is
  # available.
  # 
  # IO.popen("which #{bin}") { |io| return io.readline.chomp }

  return nil
end