<commit_before>module RSpec
  module Matchers
    module BuiltIn
      class BeWithin
        include BaseMatcher

        attr_reader :delta

        def initialize(delta)
          @delta = delta
        end

        def matches?(actual)
          check_presence_of_expected
          check_actual_is_numeric(actual)

          (super(actual) - expected).abs <= delta
        end

        def of(expected)
          @expected = expected
          self
        end

        def failure_message_for_should
          "expected #{actual} to #{description}"
        end

        def failure_message_for_should_not
          "expected #{actual} not to #{description}"
        end

        def description
          "be within #{delta} of #{expected}"
        end

        private

        def check_presence_of_expected
          unless defined?(@expected)
            raise ArgumentError.new("You must set an expected value using #of: be_within(#{delta}).of(expected_value)")
          end
        end

        def check_actual_is_numeric(actual)
          unless actual.is_a? Numeric
            raise ArgumentError, "The actual value (#{actual.inspect}) must be of a `Numeric` type"
          end
        end
      end
    end
  end
end<commit_msg>refactor BeWithin to be more self-contained.<commit_after>module RSpec
  module Matchers
    module BuiltIn
      class BeWithin
        include BaseMatcher

        def initialize(delta)
          @delta = delta
        end

        def matches?(actual)
          @actual = actual
          raise needs_expected unless defined? @expected 
          raise needs_numeric  unless @actual.is_a? Numeric 
          (@actual - @expected).abs <= @delta
        end

        def of(expected)
          @expected = expected
          self
        end

        def failure_message_for_should
          "expected #{@actual} to #{description}"
        end

        def failure_message_for_should_not
          "expected #{@actual} not to #{description}"
        end

        def description
          "be within #{@delta} of #{@expected}"
        end

        private

        def needs_numeric
          ArgumentError.new "The actual value (#{@actual.inspect}) must be of a `Numeric` type"
        end

        def needs_expected
          ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)"
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.06989247311827956, 'fraction_numerical': 0.0, 'mean_word_length': 1.822637106184364, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7378405', 'n_tokens_mistral': 641, 'n_tokens_neox': 614, 'n_words': 192}