summaryrefslogtreecommitdiff
path: root/ext/psych/lib
diff options
context:
space:
mode:
Diffstat (limited to 'ext/psych/lib')
-rw-r--r--ext/psych/lib/psych.rb22
-rw-r--r--ext/psych/lib/psych/core_ext.rb5
-rw-r--r--ext/psych/lib/psych/nodes/node.rb4
-rw-r--r--ext/psych/lib/psych/visitors/emitter.rb4
4 files changed, 28 insertions, 7 deletions
diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb
index c1feff8e3a..ba4a683974 100644
--- a/ext/psych/lib/psych.rb
+++ b/ext/psych/lib/psych.rb
@@ -155,11 +155,29 @@ module Psych
end
###
- # Dump Ruby object +o+ to a YAML string using +options+.
+ # call-seq:
+ # Psych.dump(o) -> string of yaml
+ # Psych.dump(o, options) -> string of yaml
+ # Psych.dump(o, io) -> io object passed in
+ # Psych.dump(o, io, options) -> io object passed in
+ #
+ # 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.
#
# Example:
#
+ # # Dump an array, get back a YAML string
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
+ #
+ # # Dump an array to an IO object
+ # Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
+ #
+ # # Dump an array with indentation set
+ # Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
+ #
+ # # Dump an array to an IO with indentation set
+ # Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
def self.dump o, io = nil, options = {}
if Hash === io
options = io
@@ -168,7 +186,7 @@ module Psych
visitor = Psych::Visitors::YAMLTree.new options
visitor << o
- visitor.tree.to_yaml io
+ visitor.tree.to_yaml io, options
end
###
diff --git a/ext/psych/lib/psych/core_ext.rb b/ext/psych/lib/psych/core_ext.rb
index 8d3e8fb7b6..be26b2f449 100644
--- a/ext/psych/lib/psych/core_ext.rb
+++ b/ext/psych/lib/psych/core_ext.rb
@@ -6,9 +6,10 @@ class Object
# FIXME: rename this to "to_yaml" when syck is removed
###
- # call-seq: to_yaml
+ # call-seq: to_yaml(options = {})
#
- # Convert an object to YAML
+ # Convert an object to YAML. See Psych.dump for more information on the
+ # available +options+.
def psych_to_yaml options = {}
Psych.dump self, options
end
diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb
index 3ab9acae43..bfd91f99ec 100644
--- a/ext/psych/lib/psych/nodes/node.rb
+++ b/ext/psych/lib/psych/nodes/node.rb
@@ -30,10 +30,10 @@ module Psych
# Convert this node to YAML.
#
# See also Psych::Visitors::Emitter
- def to_yaml io = nil
+ def to_yaml io = nil, options = {}
real_io = io || StringIO.new
- Visitors::Emitter.new(real_io).accept self
+ Visitors::Emitter.new(real_io, options).accept self
return real_io.string unless io
io
end
diff --git a/ext/psych/lib/psych/visitors/emitter.rb b/ext/psych/lib/psych/visitors/emitter.rb
index 0768fbb528..c84629f9d0 100644
--- a/ext/psych/lib/psych/visitors/emitter.rb
+++ b/ext/psych/lib/psych/visitors/emitter.rb
@@ -1,8 +1,10 @@
module Psych
module Visitors
class Emitter < Psych::Visitors::Visitor
- def initialize io
+ def initialize io, options = {}
@handler = Psych::Emitter.new io
+ @handler.indentation = options[:indentation] if options[:indentation]
+ @handler.canonical = options[:canonical] if options[:canonical]
end
def visit_Psych_Nodes_Stream o