awesome-print/awesome_print · warning

(Object doesn't support #ai)

Error message

(Object doesn't support #ai)

What it means

This message comes from awesome_print's IRB integration: AwesomePrint.irb! (custom_defaults.rb:28, usual_rb) monkey-patches IRB::Irb#output_value so that after every statement, IRB prints the result with ap @context.last_value instead of the stock inspector. ap (core_ext/kernel.rb:20) calls object.ai, a method awesome_print defines on Kernel — so any value that does not have Kernel's methods, most commonly a BasicObject/BlankSlate instance, raises NoMethodError, which this rescue converts into the placeholder line. The session keeps running; only that one value is not displayed. It never happens from a plain ap obj in scripts — there the NoMethodError propagates to your code normally.

Source

Thrown at lib/awesome_print/custom_defaults.rb:33

    def rails_console?
      console? && boolean(defined?(Rails::Console) || ENV['RAILS_ENV'])
    end

    def diet_rb
      IRB.formatter = Class.new(IRB::Formatter) do
        def inspect_object(object)
          object.ai
        end
      end.new
    end

    def usual_rb
      IRB::Irb.class_eval do
        def output_value(*args)
          ap @context.last_value
        rescue NoMethodError
          puts "(Object doesn't support #ai)"
        end
      end
    end

    def irb!
      return unless defined?(IRB)

      IRB.version.include?('DietRB') ? diet_rb : usual_rb
    end

    def pry!
      Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry)
    end

    private

    # Takes a value and returns true unless it is false or nil
    # This is an alternative to the less readable !!(value)

View on GitHub (pinned to 8a7ff0aaba)

Solutions

  1. Treat it as informational: re-run the expression into a local (v = expr) and print through Kernel directly — Kernel.instance_method(:inspect).bind(v).call — because BasicObject intentionally has no methods, not even #inspect or #to_s.
  2. If the object is a library proxy, use that library's own rendering API (its documented to_s wrapper or inspector) instead of relying on console auto-print.
  3. Stop auto-patching IRB: remove AwesomePrint.irb! from ~/.irbrc / ~/.aprc so stock IRB result printing is restored, and call ap obj explicitly only where you want it.
  4. Replace the patch with a guarded one in ~/.irbrc that checks #ai via Kernel binding and falls back to plain printing for objects that lack it.

Example fix

# before (~/.irbrc) — every console result is forced through #ai
require 'awesome_print'
AwesomePrint.irb!

# after — keep stock IRB printing; pretty-print on demand
require 'awesome_print'
# ap my_object  # explicit, only for objects that support #ai
Defensive patterns

Strategy: type-guard

Validate before calling

# Before printing an arbitrary value with ap/ai (works even for BasicObject)
printable = begin
  Kernel.instance_method(:respond_to?).bind(value).call(:ai)
rescue TypeError, NoMethodError
  false
end
ap(value) if printable

Type guard

def ap_printable?(obj)
  Kernel.instance_method(:respond_to?).bind(obj).call(:ai)
rescue StandardError
  false # BasicObject and stripped objects land here (bind raises TypeError for non-Kernel instances)
end

Try / catch

begin
  ap value
rescue NoMethodError
  # BasicObject has no inspect/to_s either — go through Kernel
  puts Kernel.instance_method(:inspect).bind(value).call
end

Prevention

When it happens

Trigger: An IRB or rails console session started with the awesome_print patch active (require 'awesome_print'; AwesomePrint.irb! — typically in ~/.irbrc or ~/.aprc), then evaluating an expression whose return value lacks Kernel#ai: BasicObject.new, a library BasicObject/blank-slate proxy (DSL wrappers, null objects), or an object that undefs ai or whose method_missing raises NoMethodError. Each such statement prints '(Object doesn't support #ai)' instead of => value.

Common situations: Consoles configured to auto-pretty-print every result while exploring code built on BasicObject proxies or null-object patterns; DSL gems exposing blank-slate objects; .irbrc shared across projects where one project's decorators behave differently; developers mistaking the line for a crash inside their own code.

Related errors


AI-assisted analysis of awesome-print/awesome_print@8a7ff0aaba (2026-08-23). Data as JSON: /api/errors/fe20de921e95c184. Report an issue: GitHub.