ruby/ruby · error · TypeError

no implicit conversion of #{json.class} into String

Error message

 no implicit conversion of #{json.class} into String

What it means

Raised by Gem::Package::Old#spec (outer rescue ArgumentError branch) with the same 'Failed to parse gem specification out of gem file' message, but for ArgumentError from Gem::Specification.from_yaml instead of Psych::SyntaxError. Psych/SafeYAML raises ArgumentError for structural problems that are syntactically valid YAML but invalid as a specification — unknown aliases/tags, unexpected object shapes, or non-string keys under safe loading.

Source

Thrown at ext/json/lib/json/common.rb:186

        "#{super_message}\nInvalid object: #{@invalid_object.inspect}"
      end
    end
  end

  # Fragment of JSON document that is to be included as is:
  #   fragment = JSON::Fragment.new("[1, 2, 3]")
  #   JSON.generate({ count: 3, items: fragments })
  #
  # This allows to easily assemble multiple JSON fragments that have
  # been persisted somewhere without having to parse them nor resorting
  # to string interpolation.
  #
  # Note: no validation is performed on the provided string. It is the
  # responsibility of the caller to ensure the string contains valid JSON.
  Fragment = Struct.new(:json) do
    def initialize(json)
      unless string = String.try_convert(json)
        raise TypeError, " no implicit conversion of #{json.class} into String"
      end

      super(string)
    end

    def to_json(state = nil, *)
      json
    end
  end

  module_function

  # :call-seq:
  #   JSON.parse(source, opts) -> object
  #
  # Returns the Ruby objects created by parsing the given +source+.
  #
  # Argument +source+ contains the \String to be parsed.

View on GitHub (pinned to 0e5b888e1c)

Solutions

  1. Run a modern re-release of the gem instead of the pre-1.0 artifact
  2. If inspection is required, extract the YAML block and load it with Psych.safe_load per-field to find the rejected construct
  3. Re-download in case of corruption
  4. Report the gem to its mirror if the artifact is provably damaged
Defensive patterns

Strategy: try-catch

Validate before calling

# probe whether the spec YAML survives safe loading before install
require 'psych'
Psych.safe_load(extract_spec_section(File.read(path)), permitted_classes: [Date, Time, Gem::Dependency], aliases: false)

Try / catch

begin
  Gem::Package::Old.new(io).spec
rescue Gem::Exception => e
  warn "old gem uses YAML unsafe to load: #{e.message}"
end

Prevention

When it happens

Trigger: Old-format gem whose YAML parses but does not load: e.g. YAML referencing an undefined alias, a gemspec serialized with Ruby-object tags (!ruby/object) that safe loading rejects, or a spec whose top-level structure is a sequence instead of a mapping. Distinct from error 513: the YAML text itself is well-formed.

Common situations: Gems packaged by very old Ruby/RubyGems whose YAML serializer emitted constructs modern Psych refuses; hand-merged spec YAML from different gems; security hardening (safe_yaml) rejecting object deserialization that old Ruby allowed.

Related errors


AI-assisted analysis of ruby/ruby@0e5b888e1c (2026-08-21). Data as JSON: /api/errors/d62031043e42fb35. Report an issue: GitHub.