summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-10-05 14:14:39 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-10-05 14:14:39 -0700
commita5aa707138e05d42b7448c1ab2e229ac887506e5 (patch)
treea999c1bc407517adcd78b76afd1c80161ace2378
parent3d54485fc149b65344db0272df0ed1820bbb03f3 (diff)
downloadpsych-a5aa707138e05d42b7448c1ab2e229ac887506e5.zip
fixing complex numbers in 1.9
-rw-r--r--lib/psych/ruby.rb1
-rw-r--r--lib/psych/visitors/to_ruby.rb7
-rw-r--r--lib/psych/visitors/yast_builder.rb6
-rw-r--r--test/visitors/test_to_ruby.rb13
4 files changed, 19 insertions, 8 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index ae5dbce..e68632b 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -1,4 +1,5 @@
require 'complex'
+require 'rational'
[
Object, String, Class, Hash, Array, NilClass, Float,
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index da94ebb..7cfe333 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -14,6 +14,7 @@ module Psych
@st[o.anchor] = o.value if o.anchor
return o.value if ['!str', 'tag:yaml.org,2002:str'].include?(o.tag)
+ return Complex(o.value) if o.tag == "!ruby/object:Complex"
return o.value if o.quoted
token = ScalarScanner.new(o.value).tokenize
@@ -36,15 +37,15 @@ module Psych
def visit_Psych_Nodes_Mapping o
case o.tag
- when 'ruby/range'
+ when '!ruby/range'
h = Hash[*o.children.map { |c| accept c }]
Range.new(h['begin'], h['end'], h['excl'])
- when 'ruby/object:Complex'
+ when '!ruby/object:Complex'
h = Hash[*o.children.map { |c| accept c }]
Complex(h['real'], h['image'])
- when 'ruby/object:Rational'
+ when '!ruby/object:Rational'
h = Hash[*o.children.map { |c| accept c }]
Rational(h['numerator'], h['denominator'])
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index a886af4..c0396e2 100644
--- a/lib/psych/visitors/yast_builder.rb
+++ b/lib/psych/visitors/yast_builder.rb
@@ -22,7 +22,7 @@ module Psych
end
def visit_Rational o
- @stack.push append Nodes::Mapping.new(nil,'ruby/object:Rational',false)
+ @stack.push append Nodes::Mapping.new(nil,'!ruby/object:Rational',false)
['denominator', o.denominator, 'numerator', o.numerator].each do |m|
accept m
end
@@ -30,7 +30,7 @@ module Psych
end
def visit_Complex o
- @stack.push append Nodes::Mapping.new(nil, 'ruby/object:Complex', false)
+ @stack.push append Nodes::Mapping.new(nil, '!ruby/object:Complex', false)
['real', o.real, 'image', o.image].each do |m|
accept m
end
@@ -71,7 +71,7 @@ module Psych
end
def visit_Range o
- @stack.push append Nodes::Mapping.new(nil, 'ruby/range', false)
+ @stack.push append Nodes::Mapping.new(nil, '!ruby/range', false)
['begin', o.begin, 'end', o.end, 'excl', o.exclude_end?].each do |m|
accept m
end
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index fa7c539..3cb1ead 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -1,6 +1,8 @@
require 'minitest/autorun'
require 'psych'
require 'complex'
+require 'date'
+require 'rational'
module Psych
module Visitors
@@ -19,7 +21,7 @@ module Psych
end
def test_rational
- mapping = Nodes::Mapping.new nil, 'ruby/object:Rational'
+ mapping = Nodes::Mapping.new nil, '!ruby/object:Rational'
mapping.children << Nodes::Scalar.new('denominator')
mapping.children << Nodes::Scalar.new('2')
mapping.children << Nodes::Scalar.new('numerator')
@@ -29,7 +31,7 @@ module Psych
end
def test_complex
- mapping = Nodes::Mapping.new nil, 'ruby/object:Complex'
+ mapping = Nodes::Mapping.new nil, '!ruby/object:Complex'
mapping.children << Nodes::Scalar.new('image')
mapping.children << Nodes::Scalar.new('2')
mapping.children << Nodes::Scalar.new('real')
@@ -38,6 +40,13 @@ module Psych
assert_equal Complex(1,2), mapping.to_ruby
end
+ if RUBY_VERSION >= '1.9'
+ def test_complex_string
+ node = Nodes::Scalar.new '3+4i', nil, "!ruby/object:Complex"
+ assert_equal Complex(3, 4), node.to_ruby
+ end
+ end
+
def test_integer
i = Nodes::Scalar.new('1', nil, 'tag:yaml.org,2002:int')
assert_equal 1, i.to_ruby