ruby-changes:15252
From: tenderlove <ko1@a...>
Date: Thu, 1 Apr 2010 06:12:16 +0900 (JST)
Subject: [ruby-changes:15252] Ruby:r27134 (trunk): * ext/psych/lib/psych.rb: Syck api compatibility
tenderlove 2010-04-01 06:09:58 +0900 (Thu, 01 Apr 2010) New Revision: 27134 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=27134 Log: * ext/psych/lib/psych.rb: Syck api compatibility [ruby-core:29157] * ext/psych/lib/psych/nodes/node.rb: ditto * test/psych/test_psych.rb: ditto Modified files: trunk/ext/psych/lib/psych/nodes/node.rb trunk/ext/psych/lib/psych.rb trunk/test/psych/test_psych.rb Index: ext/psych/lib/psych/nodes/node.rb =================================================================== --- ext/psych/lib/psych/nodes/node.rb (revision 27133) +++ ext/psych/lib/psych/nodes/node.rb (revision 27134) @@ -30,10 +30,12 @@ # Convert this node to YAML. # # See also Psych::Visitors::Emitter - def to_yaml - io = StringIO.new - Visitors::Emitter.new(io).accept self - io.string + def to_yaml io = nil + real_io = io || StringIO.new + + Visitors::Emitter.new(real_io).accept self + return real_io.string unless io + io end end end Index: ext/psych/lib/psych.rb =================================================================== --- ext/psych/lib/psych.rb (revision 27133) +++ ext/psych/lib/psych.rb (revision 27134) @@ -153,10 +153,15 @@ # Example: # # Psych.dump(['a', 'b']) # => "---\n- a\n- b\n" - def self.dump o, options = {} + def self.dump o, io = nil, options = {} + if Hash === io + options = io + io = nil + end + visitor = Psych::Visitors::YAMLTree.new options visitor << o - visitor.tree.to_yaml + visitor.tree.to_yaml io end ### Index: test/psych/test_psych.rb =================================================================== --- test/psych/test_psych.rb (revision 27133) +++ test/psych/test_psych.rb (revision 27134) @@ -1,5 +1,8 @@ require_relative 'helper' +require 'stringio' +require 'tempfile' + class TestPsych < Psych::TestCase def test_dump_stream things = [22, "foo \n", {}] @@ -7,6 +10,22 @@ assert_equal things, Psych.load_stream(stream) end + def test_dump_file + hash = {'hello' => 'TGIF!'} + Tempfile.open('fun.yml') do |io| + assert_equal io, Psych.dump(hash, io) + io.rewind + assert_equal Psych.dump(hash), io.read + end + end + + def test_dump_io + hash = {'hello' => 'TGIF!'} + stringio = StringIO.new '' + assert_equal stringio, Psych.dump(hash, stringio) + assert_equal Psych.dump(hash), stringio.string + end + def test_simple assert_equal 'foo', Psych.load("--- foo\n") end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/