123456789_123456789_123456789_123456789_123456789_

Module: ActiveSupport::InspectBackport

Do not use. This module is for internal use only.
Relationships & Source Files
Defined in: activesupport/lib/active_support/inspect_backport.rb

Overview

Provides a Ruby 4.0-compatible #inspect method for Ruby < 4.0.

Ruby 4.0 introduced instance_variables_to_inspect, which lets classes control which instance variables appear in #inspect output without overriding #inspect entirely. This module backports that behavior so classes can define instance_variables_to_inspect on any Ruby version.

class MyClass
  ActiveSupport::InspectBackport.apply(self)

  private
    def instance_variables_to_inspect
      [:@name, :@status].freeze
    end
end

Class Method Summary

  • .apply(_)

    See additional method definition at line 22.

Instance Method Summary

Class Method Details

.apply(_)

See additional method definition at line 22.

[ GitHub ]

  
# File 'activesupport/lib/active_support/inspect_backport.rb', line 26

def apply(klass)
  klass.define_method(:inspect, instance_method(:inspect))
end

Instance Method Details

#inspect

[ GitHub ]

  
# File 'activesupport/lib/active_support/inspect_backport.rb', line 32

def inspect
  ivars = instance_variables_to_inspect
  klass = self.class.name || self.class.inspect
  addr = "0x%x" % object_id

  if ivars.empty?
    "#<#{klass}:#{addr}>"
  else
    pairs = ivars.filter_map do |ivar|
      if instance_variable_defined?(ivar)
        "#{ivar}=#{instance_variable_get(ivar).inspect}"
      end
    end
    "#<#{klass}:#{addr} #{pairs.join(", ")}>"
  end
end