{"record":{"id":"dd65f4faa06f571f","repo":"ruby/ruby","slug":"ruby-code-case-not-handled-obj-class","errorCode":null,"errorMessage":"ruby_code case not handled: #{obj.class}","messagePattern":"ruby_code case not handled: #(.+?)","errorType":"exception","errorClass":"Gem::Exception","httpStatus":null,"severity":"error","filePath":"lib/rubygems/specification.rb","lineNumber":2267,"sourceCode":"  # object.\n\n  def ruby_code(obj)\n    case obj\n    when String             then obj.dump + \".freeze\"\n    when Array              then \"[\" + obj.map {|x| ruby_code x }.join(\", \") + \"]\"\n    when Hash               then\n      seg = obj.keys.sort.map {|k| \"#{k.to_s.dump} => #{obj[k].to_s.dump}\" }\n      \"{ #{seg.join(\", \")} }\"\n    when Gem::Version       then ruby_code(obj.to_s)\n    when DateLike           then obj.strftime(\"%Y-%m-%d\").dump\n    when Time               then obj.strftime(\"%Y-%m-%d\").dump\n    when Numeric            then obj.inspect\n    when true, false, nil   then obj.inspect\n    when Gem::Platform      then \"Gem::Platform.new(#{ruby_code obj.to_a})\"\n    when Gem::Requirement   then\n      list = obj.as_list\n      \"Gem::Requirement.new(#{ruby_code(list.size == 1 ? obj.to_s : list)})\"\n    else raise Gem::Exception, \"ruby_code case not handled: #{obj.class}\"\n    end\n  end\n\n  private :ruby_code\n\n  ##\n  # List of dependencies that will automatically be activated at runtime.\n\n  def runtime_dependencies\n    dependencies.select(&:runtime?)\n  end\n\n  ##\n  # True if this gem has the same attributes as +other+.\n\n  def same_attributes?(spec)\n    @@attributes.all? {|name, _default| send(name) == spec.send(name) }\n  end","sourceCodeStart":2249,"sourceCodeEnd":2285,"githubUrl":"https://github.com/ruby/ruby/blob/0e5b888e1c355f3f728f2659f085820937dada48/lib/rubygems/specification.rb#L2249-L2285","documentation":"ruby_code (used by Gem::Specification#to_ruby and gem specification --ruby) renders each attribute value as Ruby source through a case over known classes: String/Array/Hash, Gem::Version, DateLike, Time, Numeric, true/false/nil, Gem::Platform, Gem::Requirement. Any other class on a specification attribute falls through and raises — the serializer simply has no branch for that type.","triggerScenarios":"spec.to_ruby / gem spec pkg/x-1.0.gem --ruby when a specification field holds an object outside the handled set — e.g. a custom class or subclass assigned to an attribute, odd objects introduced by YAML/Marshal round-trips, or types supported only by newer RubyGems versions.","commonSituations":"Regenerating a gemspec from an installed/built gem after third-party code mutated spec fields; very old marshalled specs with legacy attribute types; automation that assigns convenience objects to spec attributes.","solutions":["Upgrade RubyGems — serialization branches for additional classes have been added over releases","Find and normalize the offending field: walk spec's instance variables and convert anything that is not a supported type (String, Array, Hash, Gem::Version, Date/Time, Numeric, Gem::Platform, Gem::Requirement) before calling to_ruby","Stop assigning custom objects to Gem::Specification attributes; store extras in metadata as plain strings"],"exampleFix":"# before\nspec.metadata_pointing_at_custom = MyCustomValue.new(x)\nputs spec.to_ruby  #=> ruby_code case not handled: MyCustomValue\n\n# after\nspec.metadata[\"custom\"] = \"plain string\"  # keep attribute values serializable\nputs spec.to_ruby","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"SUPPORTED = [String, Array, Hash, Gem::Version, Date, Time, Numeric, TrueClass, FalseClass, NilClass, Gem::Platform, Gem::Requirement]\ndef to_ruby_safe?(spec)\n  spec.instance_variables.all? do |iv|\n    v = spec.instance_variable_get(iv)\n    v.nil? || SUPPORTED.any? {|k| v.is_a?(k) }\n  end\nend","tryCatchPattern":null,"preventionTips":["Keep specification attribute values limited to the classes ruby_code handles; put custom data in spec.metadata as strings","Upgrade RubyGems before reporting — new serialization branches land in newer releases","When generating gemspecs programmatically (to_ruby), validate attribute types first and convert or stringify anything exotic"],"tags":["rubygems","gemspec","serialization","to-ruby"],"backgroundTag":"unsupported-type-serialization","analyzedSha":"0e5b888e1c355f3f728f2659f085820937dada48","analyzedAt":"2026-08-21T14:25:43.473Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}