ruby-changes:8649
From: yugui <ko1@a...>
Date: Tue, 11 Nov 2008 17:41:43 +0900 (JST)
Subject: [ruby-changes:8649] Ruby:r20184 (ruby_1_9_1): merges r20126 from trunk into ruby_1_9_1.
yugui 2008-11-11 17:41:13 +0900 (Tue, 11 Nov 2008) New Revision: 20184 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20184 Log: merges r20126 from trunk into ruby_1_9_1. * lib/yaml/rubytypes.rb: support Rational and Complex as 1.8 does. a patch from Hiroshi Moriyama in [ruby-dev:36899]. Modified files: branches/ruby_1_9_1/ChangeLog branches/ruby_1_9_1/lib/yaml/rubytypes.rb branches/ruby_1_9_1/test/yaml/test_yaml.rb Index: ruby_1_9_1/ChangeLog =================================================================== --- ruby_1_9_1/ChangeLog (revision 20183) +++ ruby_1_9_1/ChangeLog (revision 20184) @@ -1,3 +1,8 @@ +Fri Nov 7 07:32:55 2008 Yukihiro Matsumoto <matz@r...> + + * lib/yaml/rubytypes.rb: support Rational and Complex as 1.8 + does. a patch from Hiroshi Moriyama in [ruby-dev:36899]. + Fri Nov 7 06:58:59 2008 Yukihiro Matsumoto <matz@r...> * string.c (Init_String): remove Symbol#===. [ruby-dev:37026] Index: ruby_1_9_1/lib/yaml/rubytypes.rb =================================================================== --- ruby_1_9_1/lib/yaml/rubytypes.rb (revision 20183) +++ ruby_1_9_1/lib/yaml/rubytypes.rb (revision 20184) @@ -379,6 +379,44 @@ end end +class Rational + yaml_as "tag:ruby.yaml.org,2002:object:Rational" + def Rational.yaml_new( klass, tag, val ) + if val.is_a? String + Rational( val ) + else + Rational( val['numerator'], val['denominator'] ) + end + end + def to_yaml( opts = {} ) + YAML::quick_emit( self, opts ) do |out| + out.map( taguri, nil ) do |map| + map.add( 'denominator', denominator ) + map.add( 'numerator', numerator ) + end + end + end +end + +class Complex + yaml_as "tag:ruby.yaml.org,2002:object:Complex" + def Complex.yaml_new( klass, tag, val ) + if val.is_a? String + Complex( val ) + else + Complex( val['real'], val['image'] ) + end + end + def to_yaml( opts = {} ) + YAML::quick_emit( self, opts ) do |out| + out.map( taguri, nil ) do |map| + map.add( 'image', imaginary ) + map.add( 'real', real ) + end + end + end +end + class TrueClass yaml_as "tag:yaml.org,2002:bool#yes" def to_yaml( opts = {} ) Index: ruby_1_9_1/test/yaml/test_yaml.rb =================================================================== --- ruby_1_9_1/test/yaml/test_yaml.rb (revision 20183) +++ ruby_1_9_1/test/yaml/test_yaml.rb (revision 20184) @@ -1107,6 +1107,30 @@ end + def test_ruby_rational + assert_to_yaml( Rational(1, 2), <<EOY ) +--- !ruby/object:Rational +numerator: 1 +denominator: 2 +EOY + + # Read YAML dumped by the ruby 1.8.3. + assert_to_yaml( Rational(1, 2), "!ruby/object:Rational 1/2\n" ) + assert_raise( ArgumentError ) { YAML.load("!ruby/object:Rational INVALID/RATIONAL\n") } + end + + def test_ruby_complex + assert_to_yaml( Complex(3, 4), <<EOY ) +--- !ruby/object:Complex +image: 4 +real: 3 +EOY + + # Read YAML dumped by the ruby 1.8.3. + assert_to_yaml( Complex(3, 4), "!ruby/object:Complex 3+4i\n" ) + assert_raise( ArgumentError ) { YAML.load("!ruby/object:Complex INVALID+COMPLEXi\n") } + end + def test_emitting_indicators assert_to_yaml( "Hi, from Object 1. You passed: please, pretty please", <<EOY --- "Hi, from Object 1. You passed: please, pretty please" -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/