Home > Net >  Upgrading to Ruby 3.1 causes Psych::DisallowedClass exception when using YAML.load_file
Upgrading to Ruby 3.1 causes Psych::DisallowedClass exception when using YAML.load_file

Time:03-03

When upgrading to ruby 3.1, I am seeing the following sort error message when using YAML.load_file some_file_name

 Psych::DisallowedClass:
   Tried to load unspecified class: Matrix

Other load statements causes similar errors but cite different unspecified clasees e.g. OpenStruct. It appears that the latest version of YAML only loads classes from a permitted white list, so it is necessary to use a permitted_class keyword to allow other classes. I have tried

hsh = YAML.load_file some_file_name, permitted_classes: [Matrix, OpenStruct]

but this gives the error

 Psych::DisallowedClass:
   Tried to load unspecified class: Symbol

how do I fix this.

CodePudding user response:

Symbol is also not allowed per default. Therefore just add Symbol to the permitted_classes too:

hash = YAML.load_file(
  some_file_name, 
  permitted_classes: [Matrix, OpenStruct, Symbol]
)

See the list of default permitted_classes.

  • Related