From 1343680398592089929607a3d213f731847a7762 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 6 Nov 2008 22:36:07 +0000 Subject: * 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 --- lib/yaml/rubytypes.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib/yaml') 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 = {} ) -- cgit v1.2.3