<commit_before>class NameCollisionError < RuntimeError; end
class NameFormatError < RuntimeError; end

class Robot
  attr_accessor :name

  @@registry

  def initialize(args = {})
    @@registry ||= []
    @name_generator = args[:name_generator] || proc { generate_name }
    @name = @name_generator.call
    check_name
    @@registry << @name
  end

  def generate_name
    temp_name = ''
      2.times { temp_name << ('A'..'Z').to_a.sample }
      3.times { temp_name << rand(10).to_s }
    temp_name
  end

  def check_name
    check_name_format
    check_name_collision
  end

  def check_name_format
    unless name =~ /[[:alpha:]]{2}[[:digit:]]{3}/
      raise NameFormatError,
        'Robot name should be 2 letters and 3 numbers. Ex: AA111'
    end
  end

  def check_name_collision
    if @@registry.include?(name)
      raise NameCollisionError,
        'Robot name is already in the registry'
    end
  end
end

robot = Robot.new
puts "My pet robot's name is #{robot.name}, but we usually call him sparky."

# Errors!
begin
generator = -> { 'AA111' }
Robot.new(name_generator: generator)
Robot.new(name_generator: generator)
rescue => e
  puts "Failed with: #{e.class}: #{e.message}"
end

begin
generator = -> { 'AA11' }
Robot.new(name_generator: generator)
rescue => e
  puts "Failed with: #{e.class}: #{e.message}"
end
<commit_msg>Improve format error to include name<commit_after>class NameCollisionError < RuntimeError; end
class NameFormatError < RuntimeError; end

class Robot
  attr_accessor :name

  @@registry

  def initialize(args = {})
    @@registry ||= []
    @name_generator = args[:name_generator] || proc { generate_name }
    @name = @name_generator.call
    check_name
    @@registry << @name
  end

  def generate_name
    temp_name = ''
      2.times { temp_name << ('A'..'Z').to_a.sample }
      3.times { temp_name << rand(10).to_s }
    temp_name
  end

  def check_name
    check_name_format
    check_name_collision
  end

  def check_name_format
    unless name =~ /[[:alpha:]]{2}[[:digit:]]{3}/
      raise NameFormatError,
        "Robot name: #{name} should be 2 letters and 3 numbers. Ex: AA111"
    end
  end

  def check_name_collision
    if @@registry.include?(name)
      raise NameCollisionError,
        'Robot name is already in the registry'
    end
  end
end

robot = Robot.new
puts "My pet robot's name is #{robot.name}, but we usually call him sparky."

# Errors!
begin
generator = -> { 'AA111' }
Robot.new(name_generator: generator)
Robot.new(name_generator: generator)
rescue => e
  puts "Failed with: #{e.class}: #{e.message}"
end

begin
generator = -> { 'AA11' }
Robot.new(name_generator: generator)
rescue => e
  puts "Failed with: #{e.class}: #{e.message}"
end

{'fraction_non_alphanumeric': 0.12417340191036003, 'fraction_numerical': 0.011756061719324026, 'mean_word_length': 3.3568, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, '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': '2726158', 'n_tokens_mistral': 952, 'n_tokens_neox': 914, 'n_words': 250}