Sha256: 5fc2731dc34cd025a6cfb7ad38d15179d3dfd7ce582edff53e9de51513e3346f

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

module Datadog
  module Core
    module Utils
      # Helper methods for safer dup
      module SafeDup
        if RUBY_VERSION < '2.2' # nil.dup only fails in Ruby 2.1
          # Ensures #initialize can call nil.dup safely
          module RefineNil
            refine NilClass do
              def dup
                self
              end
            end
          end

          using RefineNil
        end

        # String#+@ was introduced in Ruby 2.3
        if String.method_defined?(:+@) && String.method_defined?(:-@)
          def self.frozen_or_dup(v)
            # If the string is not frozen, the +(-v) will:
            # - first create a frozen deduplicated copy with -v
            # - then it will dup it more efficiently with +v
            v.frozen? ? v : +(-v)
          end

          def self.frozen_dup(v)
            -v if v
          end
        else
          def self.frozen_or_dup(v)
            v.frozen? ? v : v.dup
          end

          def self.frozen_dup(v)
            v.frozen? ? v : v.dup.freeze
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-1.12.1 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.12.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.11.1 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.11.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.11.0.beta1 lib/datadog/core/utils/safe_dup.rb