summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-06 22:36:07 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-06 22:36:07 +0000
commit1343680398592089929607a3d213f731847a7762 (patch)
tree4b228174576bb81459dc6c7b93049adfce417293
parent1d8b7bc6248931354e9e018296bb1ad1abbae6fd (diff)
* lib/yaml/rubytypes.rb: support Rational and Complex as 1.8
does. a patch from Hiroshi Moriyama in [ruby-dev:36899]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--lib/yaml/rubytypes.rb38
-rw-r--r--test/yaml/test_yaml.rb24
3 files changed, 67 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 28bcd991e3..759f73456d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Nov 7 07:32:55 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * lib/yaml/rubytypes.rb: support Rational and Complex as 1.8
+ does. a patch from Hiroshi Moriyama in [ruby-dev:36899].
+
Fri Nov 7 07:12:06 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* thread_pthread.c (thread_timer): uses pthread_cond_timedwait always
diff --git a/lib/yaml/rubytypes.rb b/lib/yaml/rubytypes.rb
index 35b719196e..ae65b355e1 100644
--- a/lib/yaml/rubytypes.rb
+++ b/lib/yaml/rubytypes.rb
@@ -379,6 +379,44 @@ class Float
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 = {} )
diff --git a/test/yaml/test_yaml.rb b/test/yaml/test_yaml.rb
index 74a2fa851e..ed94705fcf 100644
--- a/test/yaml/test_yaml.rb
+++ b/test/yaml/test_yaml.rb
@@ -1107,6 +1107,30 @@ EOY
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"