<commit_before>require 'date'
require 'singleton'

module Turf

  class Session

    include Singleton

    attr_accessor :name
    attr_accessor :history

    def initialize
      @name = 'default'
      @history = History.new
    end

    def save
      s = {history: @history.to_a}
      b = IRB.conf[:MAIN_CONTEXT].workspace.binding
      b.eval("local_variables").each { |v|
        s[v] = b.local_variable_get(v)
      }
      dir = File.join(Configuration.instance.session_dir, @name)
      Dir.mkdir(dir, 0700) unless Dir.exist?(dir)
      fname = File.join(dir, DateTime.now.iso8601)
      File.open(fname, "wb", 0400) { |f|
        f.write(Marshal.dump(s))
      }
      puts "Saved under #{fname}"
    end

    def load(name)
      dir = File.join(Configuration.instance.session_dir, name)
      fname = Dir.glob(File.join(dir, "*")).last
      if File.exist?(fname)
        puts "Loading #{fname}"
        @name = name
        s = Marshal.load(File.open(fname))
        b = IRB.conf[:MAIN_CONTEXT].workspace.binding
        s.reject{|k,v| k == :history}.each { |k,v|
          b.local_variable_set(k, v)
        }
        @history = History.new s[:history]
      end
      nil
    end

  end

  module_function

  def save
    Session.instance.save
  end

  # Ruby already uses 'load'
  def use(name)
    Session.instance.load name
  end

end<commit_msg>Accept argument for save<commit_after>require 'date'
require 'singleton'

module Turf

  class Session

    include Singleton

    attr_accessor :name
    attr_accessor :history

    def initialize
      @name = 'default'
      @history = History.new
    end

    def save
      s = {history: @history.to_a}
      b = IRB.conf[:MAIN_CONTEXT].workspace.binding
      b.eval("local_variables").each { |v|
        s[v] = b.local_variable_get(v)
      }
      dir = File.join(Configuration.instance.session_dir, @name)
      Dir.mkdir(dir, 0700) unless Dir.exist?(dir)
      fname = File.join(dir, DateTime.now.iso8601)
      File.open(fname, "wb", 0400) { |f|
        f.write(Marshal.dump(s))
      }
      puts "Saved under #{fname}"
    end

    def load(name)
      dir = File.join(Configuration.instance.session_dir, name)
      fname = Dir.glob(File.join(dir, "*")).last
      if File.exist?(fname)
        puts "Loading #{fname}"
        @name = name
        s = Marshal.load(File.open(fname))
        b = IRB.conf[:MAIN_CONTEXT].workspace.binding
        s.reject{|k,v| k == :history}.each { |k,v|
          b.local_variable_set(k, v)
        }
        @history = History.new s[:history]
      end
      nil
    end

  end

  module_function

  def save(name = nil)
    Session.instance.name = name unless name.nil?
    Session.instance.save
  end

  # Ruby already uses 'load'
  def restore(name)
    Session.instance.load name
  end

end
{'fraction_non_alphanumeric': 0.1238875044499822, 'fraction_numerical': 0.008543965824136704, 'mean_word_length': 2.321513002364066, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1957955', 'n_tokens_mistral': 979, 'n_tokens_neox': 950, 'n_words': 205}