Project

General

Profile

Bug #667 » yaml-fix-suport-for-ratoinal-and-complex.diff

hiroshi (Hiroshi MORIYAMA), 10/24/2008 02:29 PM

View differences:

lib/yaml/rubytypes.rb (working copy)
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 = {} )
test/yaml/test_yaml.rb (working copy)
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"
    (1-1/1)