From 97572035aee6f50de09e50f668071a3e0190509c Mon Sep 17 00:00:00 2001 From: APX403 Date: Fri, 6 Nov 2026 18:16:05 +0200 Subject: [PATCH] Add Psych.safe_dump and the missing load and dump keywords --- stdlib/psych/0/psych.rbs | 98 ++++++++++++++++++++++++++++++++++++--- test/stdlib/psych_test.rb | 60 +++++++++++++++++++++++- 2 files changed, 150 insertions(+), 8 deletions(-) diff --git a/stdlib/psych/0/psych.rbs b/stdlib/psych/0/psych.rbs index c085c2c0d3..e832748e35 100644 --- a/stdlib/psych/0/psych.rbs +++ b/stdlib/psych/0/psych.rbs @@ -261,8 +261,92 @@ module Psych # Psych.dump({a: "b"}, stringify_names: true) # => "---\na: b\n" # %a{annotate:rdoc:copy:Psych.dump} - def self.dump: (untyped o, ?indentation: Integer, ?line_width: Integer, ?canonical: bool, ?header: bool) -> String - | [IO] (untyped o, IO, ?indentation: Integer, ?line_width: Integer, ?canonical: bool, ?header: bool) -> IO + def self.dump: (untyped o, ?indentation: Integer, ?line_width: Integer, ?canonical: bool, ?header: bool, ?stringify_names: bool) -> String + | [IO] (untyped o, IO, ?indentation: Integer, ?line_width: Integer, ?canonical: bool, ?header: bool, ?stringify_names: bool) -> IO + + # + # Safely dump Ruby object `o` to a YAML string. Optional `options` may be passed + # in to control the output format. If an IO object is passed in, the YAML will + # be dumped to that IO object. By default, only the following classes are + # allowed to be serialized: + # + # * TrueClass + # * FalseClass + # * NilClass + # * Integer + # * Float + # * String + # * Array + # * Hash + # + # Arbitrary classes can be allowed by adding those classes to the + # `permitted_classes` keyword argument. They are additive. For example, to + # allow Date serialization: + # + # Psych.safe_dump(yaml, permitted_classes: [Date]) + # + # Now the Date class can be dumped in addition to the classes listed above. + # + # A Psych::DisallowedClass exception will be raised if the object contains a + # class that isn't in the `permitted_classes` list. + # + # Currently supported options are: + # + # :indentation + # : Number of space characters used to indent. Acceptable value should be in + # 0..9 range, otherwise option is ignored. + # + # Default: `2`. + # + # :line_width + # : Max character to wrap line at. For unlimited line width use + # -1. + # + # Default: `0` (meaning "wrap at 81"). + # + # :canonical + # : Write "canonical" YAML form (very verbose, yet strictly formal). + # + # Default: `false`. + # + # :header + # : Write %YAML [version] at the beginning of document. + # + # Default: `false`. + # + # + # :stringify_names + # : Dump symbol keys in Hash objects as string. + # + # Default: `false`. + # + # + # Example: + # + # # Dump an array, get back a YAML string + # Psych.safe_dump(['a', 'b']) # => "---\n- a\n- b\n" + # + # # Dump an array to an IO object + # Psych.safe_dump(['a', 'b'], StringIO.new) # => # + # + # # Dump an array with indentation set + # Psych.safe_dump(['a', ['b']], indentation: 3) # => "---\n- a\n- - b\n" + # + # # Dump an array to an IO with indentation set + # Psych.safe_dump(['a', ['b']], StringIO.new, indentation: 3) + # + # # Dump hash with symbol keys as string + # Psych.dump({a: "b"}, stringify_names: true) # => "---\na: b\n" + # + %a{annotate:rdoc:copy:Psych.safe_dump} + def self.safe_dump: (untyped o, ?permitted_classes: Array[Class], ?permitted_symbols: Array[Symbol], ?aliases: bool, ?indentation: Integer, ?line_width: Integer, ?canonical: bool, ?header: bool, ?stringify_names: bool) -> String + | [IO] (untyped o, IO, ?permitted_classes: Array[Class], ?permitted_symbols: Array[Symbol], ?aliases: bool, ?indentation: Integer, ?line_width: Integer, ?canonical: bool, ?header: bool, ?stringify_names: bool) -> IO #