<commit_before># # Safe Chaining
require "ruvii/dependencies"

# Succint `&&` chaining.
#
# Stop writing code like:
#
#     thing && thing.foo && thing.foo.bar
#
# Instead, chain it:
#
#     thing.n.foo.n.bar
module Ruvii::SafeChaining

  # ## Implementation

  # This guy is responsible for stubbing out a fake interface for the method in question.
  #
  # It's a silent nil!  You Objective-C folk have to be happy about this.
  class ChainedNil < BasicObject

    def method_missing(sym, *args, &block)
      return nil unless nil.respond_to? sym

      nil.send(sym, *args, &block)
    end

  end

  module Object

    # Enable the happy case; we just return ourselves, and away you go!
    def n
      self
    end

  end

  module NilClass

    # The not-so-happy case.
    def n
      ChainedNil.new
    end

  end

end

Object.send   :include, Ruvii::SafeChaining::Object
NilClass.send :include, Ruvii::SafeChaining::NilClass<commit_msg>No need to instantiate a bunch of ChainedNil's<commit_after># # Safe Chaining
require "ruvii/dependencies"

# Succint `&&` chaining.
#
# Stop writing code like:
#
#     thing && thing.foo && thing.foo.bar
#
# Instead, chain it:
#
#     thing.n.foo.n.bar
module Ruvii::SafeChaining

  # ## Implementation

  # This guy is responsible for stubbing out a fake interface for the method in question.
  #
  # It's a silent nil!  You Objective-C folk have to be happy about this.
  class ChainedNil < BasicObject

    def method_missing(sym, *args, &block)
      return nil unless nil.respond_to? sym

      nil.send(sym, *args, &block)
    end

  end

  module Object

    # Enable the happy case; we just return ourselves, and away you go!
    def n
      self
    end

  end

  module NilClass

    # The not-so-happy case.
    def n
      COMMON_CHAINED_NIL
    end

  end

  # One ChainedNil behaves like any other, so let's save some cycles.
  COMMON_CHAINED_NIL = ChainedNil.new

end

Object.send   :include, Ruvii::SafeChaining::Object
NilClass.send :include, Ruvii::SafeChaining::NilClass
{'fraction_non_alphanumeric': 0.08947885939036382, 'fraction_numerical': 0.0, 'mean_word_length': 2.9514563106796117, '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': '1194098', 'n_tokens_mistral': 699, 'n_tokens_neox': 657, 'n_words': 223}