summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/psych/ruby.rb6
-rw-r--r--lib/psych/visitors/to_ruby.rb14
-rw-r--r--test/visitors/test_to_ruby.rb11
3 files changed, 27 insertions, 4 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index 6258e81..8831811 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -3,12 +3,10 @@ require 'rational'
require 'date'
[
- Object, String, Class, Hash, Array, NilClass, Float,
- FalseClass, TrueClass, Range, Complex, Rational, Date
+ Object, String, Class, Hash, Array, NilClass, Float, FalseClass, TrueClass,
+ Range, Complex, Rational, Date, Time, Regexp
# Struct
# Exception
- # Regexp
- # Time
].each do |klass|
klass.send(:remove_method, :to_yaml) rescue NameError
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 40236ce..ab8ef52 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -22,6 +22,20 @@ module Psych
Complex(o.value)
when "!ruby/object:Rational"
Rational(o.value)
+ when "!ruby/regexp"
+ o.value =~ /^\/(.*)\/([mix]*)$/
+ source = $1
+ options = 0
+ lang = nil
+ ($2 || '').split('').each do |option|
+ case option
+ when 'x' then options |= Regexp::EXTENDED
+ when 'i' then options |= Regexp::IGNORECASE
+ when 'm' then options |= Regexp::MULTILINE
+ else lang = option
+ end
+ end
+ Regexp.new(*[source, options, lang].compact)
when "!ruby/range"
args = o.value.split(/([.]{2,3})/, 2).map { |s|
accept Nodes::Scalar.new(s)
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index 41eae48..7fd99a4 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -11,6 +11,17 @@ module Psych
@visitor = ToRuby.new
end
+ def test_regexp
+ node = Nodes::Scalar.new('/foo/', nil, '!ruby/regexp')
+ assert_equal(/foo/, node.to_ruby)
+
+ node = Nodes::Scalar.new('/foo/m', nil, '!ruby/regexp')
+ assert_equal(/foo/m, node.to_ruby)
+
+ node = Nodes::Scalar.new('/foo/ix', nil, '!ruby/regexp')
+ assert_equal(/foo/ix, node.to_ruby)
+ end
+
def test_time
now = Time.now
formatted = now.strftime("%Y-%m-%d %H:%M:%S") +