<commit_before>module Gnuplot
  ##
  # === 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.
  class Datablock
    ##
    # ==== Parameters
    # * *data* - anything with +#to_points+ method
    # * *is_file* set to true will force this datablock to store its data in temporary file
    def initialize(data, is_file = false)
      @is_file = is_file
      data = data.to_points
      if @is_file
        name = Dir::Tmpname.make_tmpname('tmp_data', 0)
        File.write(name, data)
        @name = name
        ObjectSpace.define_finalizer(self, proc { File.delete(name) })
      else
        @data = data
      end
      yield(self) if block_given?
    end

    ##
    # ==== Overview
    # This will add points to datablock
    # ==== Parameters
    # * *data* - anything with +#to_points+ method
    # * *update* - set to true (by default) will try to call replot on last used terminal
    def update(data, update = true)
      if @is_file
        File.open(@name, 'a') { |f| f.puts "\n#{data.to_points}"}
      else
        @data += data.to_points
        @last_terminal.store_datablock(@name, @data) if update
      end
      @last_terminal.replot if update
    end

    ##
    # ==== Overview
    # Returns quoted filename if datablock stored in file and outputs
    # datablock to gnuplot if not
    # *gnuplot_term* should be given if datablock changes gnuplot instance
    def name(gnuplot_term = nil)
      @last_terminal ||= gnuplot_term
      if @is_file
        "'#{@name}'"
      else
        @name = @last_terminal.store_datablock(@data) unless @is_file
      end
    end

    alias_method :to_s, :name
  end
end<commit_msg>make datablock copy-on-update by default (still have forced version via #update!)<commit_after>module Gnuplot
  ##
  # === 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.
  class Datablock
    ##
    # ==== Parameters
    # * *data* - anything with +#to_points+ method
    # * *is_file* set to true will force this datablock to store its data in temporary file
    # * *options* are for inner use (to create new Datablock from existing one)
    def initialize(data, is_file = false, **options)
      @is_file = is_file
      if options[:copy_file]
        name = Dir::Tmpname.make_tmpname('tmp_data', 0)
        FileUtils.cp(options[:old_file_name], name)
        @name = name
        ObjectSpace.define_finalizer(self, proc { File.delete(name) })
        update!(data)
      else
        data = data.to_points
        if @is_file
          name = Dir::Tmpname.make_tmpname('tmp_data', 0)
          File.write(name, data)
          @name = name
          ObjectSpace.define_finalizer(self, proc { File.delete(name) })
        else
          @data = data
        end
      end
      yield(self) if block_given?
    end

    ##
    # ==== Overview
    # Add points to existing datablock
    # ==== Parameters
    # * *data* - anything with +#to_points+ method
    def update!(data)
      if @is_file
        File.open(@name, 'a') { |f| f.puts "\n#{data.to_points}"}
      else
        @data += data.to_points
      end
    end

    ##
    # ==== Overview
    # Instantiate one more Datablock with updated data
    # ==== Parameters
    # * *data* - anything with +#to_points+ method
    def update(data)
      if @is_file
        Datablock.new(data, true, copy_file: true, old_file_name: @name)
      else
        Datablock.new(@data + data.to_points, false)
      end
    end

    ##
    # ==== Overview
    # Returns quoted filename if datablock stored in file and outputs
    # datablock to gnuplot if not
    # *gnuplot_term* should be given if datablock not saved in file
    def name(gnuplot_term = nil)
      raise ArgumentError.new('No terminal given to output datablock') unless @is_file unless gnuplot_term
      @is_file ? "'#{@name}'" : gnuplot_term.store_datablock(@data)
    end

    alias_method :to_s, :name
  end
end
{'fraction_non_alphanumeric': 0.10246373474556758, 'fraction_numerical': 0.000690766751093714, 'mean_word_length': 2.517408906882591, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '946262', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1348, 'n_words': 434}