Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
ObjectSpace#dump
# File tmp/rubies/ruby-4.1.0-preview1/ext/objspace/lib/objspace.rb, line 28
def dump(obj, output: :string)
out = case output
when :file, nil
require 'tempfile'
Tempfile.create(%w(rubyobj .json))
when :stdout
STDOUT
when :string
+''
when IO
output
else
raise ArgumentError, "wrong output option: #{output.inspect}"
end
ret = _dump(obj, out)
return nil if output == :stdout
ret
end
Dump the contents of a ruby object as JSON.
output can be one of: :stdout, :file, :string, or IO object.
-
:filemeans dumping to a tempfile and returning correspondingFileobject; -
:stdoutmeans printing the dump and returningnil; -
:stringmeans returning a string with the dump; -
if an instance of
IOobject is provided, the output goes there, and the object is returned.
This method is only expected to work with C Ruby. This is an experimental method and is subject to change. In particular, the function signature and output format are not guaranteed to be compatible in future versions of ruby.