Class: GnuplotRB::Datablock

Inherits:
Object
  • Object
show all
Defined in:
lib/gnuplotrb/staff/datablock.rb

Overview

This class corresponds to points we want to plot. It may be stored in temporary file (to allow fast update) or inside “$DATA << EOD … EOD” construction. Datablock stores data passed to constructor and keeps datablock name or path to file where it is stored.

Instance Method Summary collapse

Constructor Details

#initialize(data, stored_in_file = false) ⇒ Datablock

Returns a new instance of Datablock.

Parameters:

  • data (#to_gnuplot_points)

    anything with #to_gnuplot_points method

  • stored_in_file (Boolean) (defaults to: false)

    true here will force this datablock to store its data in temporary file.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gnuplotrb/staff/datablock.rb', line 12

def initialize(data, stored_in_file = false)
  @stored_in_file = stored_in_file
  data_str = data.to_gnuplot_points
  if @stored_in_file
    @file_name = Dir.gnuplot_tmpname('tmp_data')
    File.write(@file_name, data_str)
    name = File.join(Dir.pwd, @file_name)
    ObjectSpace.define_finalizer(self, proc { File.delete(name) })
  else
    @data = data_str
  end
end

Instance Method Details

#cloneObject

Overridden #clone. Since datablock which store data in temporary files should not be cloned (otherwise it will cause double attempt to delete file), this #clone returns self for such cases. For other cases it just calls default #clone.



108
109
110
# File 'lib/gnuplotrb/staff/datablock.rb', line 108

def clone
  @stored_in_file ? self : super
end

#name(gnuplot_term = nil) ⇒ String Also known as: to_s

Get quoted filename if datablock stored in file or output datablock to gnuplot and return its name otherwise.

Parameters:

  • gnuplot_term (Terminal) (defaults to: nil)

    should be given if datablock not stored in file

Returns:

  • (String)

    quoted filename if data stored in file (see contructor)

  • (String)

    Gnuplot’s datablock name otherwise



92
93
94
95
96
97
98
99
# File 'lib/gnuplotrb/staff/datablock.rb', line 92

def name(gnuplot_term = nil)
  if @stored_in_file
    "'#{@file_name}'"
  else
    fail(ArgumentError, 'No terminal given to output datablock') unless gnuplot_term
    gnuplot_term.store_datablock(@data)
  end
end

#update(data) ⇒ Datablock

Instantiate one more Datablock with updated data if data stored in here-doc. Append update to file if data stored there.

Examples:

data = [[0, 1, 2, 3], [0, 1, 4, 9]] # y = x**2
db = Datablock.new(data, false)
update = [[4, 5], [16, 25]]
updated_db = db.update(update)
# now db and updated_db contain DIFFERENT data
# db - points with x from 0 up to 3
# updated_db - points with x from 0 to 5
data = [[0, 1, 2, 3], [0, 1, 4, 9]] # y = x**2
db = Datablock.new(data, true)
update = [[4, 5], [16, 25]]
updated_db = db.update(update)
# now db and updated_db contain THE SAME data
# because they linked with the same temporary file
# db - points with x from 0 up to 5
# updated_db - points with x from 0 to 5

Parameters:

  • data (#to_gnuplot_points)

    anything with #to_gnuplot_points method

Returns:

  • (Datablock)

    self if data stored in file (see constructor)

  • (Datablock)

    new datablock with updated data otherwise



52
53
54
55
56
57
58
59
60
# File 'lib/gnuplotrb/staff/datablock.rb', line 52

def update(data)
  data_str = data.to_gnuplot_points
  if @stored_in_file
    File.open(@file_name, 'a') { |f| f.puts "\n#{data_str}" }
    self
  else
    Datablock.new("#{@data}\n#{data_str}", false)
  end
end

#update!(data) ⇒ Datablock

Update existing Datablock with new data. Destructive version of #update.

Examples:

data = [[0, 1, 2, 3], [0, 1, 4, 9]] # y = x**2
db = Datablock.new(data, false)
update = [[4, 5], [16, 25]]
db.update!(update)
# now db contains points with x from 0 up to 5

Parameters:

  • data (#to_gnuplot_points)

    anything with #to_gnuplot_points method

Returns:



75
76
77
78
79
80
81
82
83
# File 'lib/gnuplotrb/staff/datablock.rb', line 75

def update!(data)
  data_str = data.to_gnuplot_points
  if @stored_in_file
    File.open(@file_name, 'a') { |f| f.puts "\n#{data_str}" }
  else
    @data = "#{@data}\n#{data_str}"
  end
  self
end