Pregunta

In a file is apparently stored a multiline regex, saved by YAML::dump(b1) without a problem.

Question, how can I load it back when Syck seems to have issues with the multiline regular expression?

b2 = YAML::load(File.open("browserObj.yaml", 'r'))

Syck::TypeError: Invalid Regular expression: "/\\A\\s*\n        ([a-zA-Z][\\-+.a-zA-Z\\d]*):                           (?# 1: scheme)\n        (?:\n           ((?:[\\-_.!~*'()a-zA-Z\\d;?:@&=+$,]|%[a-fA-F\\d]{2})(?:[\\-_.!~*'()a-zA-Z\\d;\\/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*) \\                   (?# 2: opaque)\n        |\n           (?:(?:\n             \\/\\/(?:\n \\                (?:(?:((?:[\\-_.!~*'()a-zA-Z\\d;:&=+$,]|%[a-fA-F\\d]{2})*)@)? \\       (?# 3: userinfo)\n                   (?:((?:(?:[a-zA-Z0-9\\-.]|%\\h\\h)+|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\[(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(?:(?:[a-fA-F\\d]{1,4}:)*[a-fA-F\\d]{1,4})?::(?:(?:[a-fA-F\\d]{1,4}:)*(?:[a-fA-F\\d]{1,4}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))?)\\]))(?::(\\d*))?))? (?# 4: host, 5: port)\n               |\n                 ((?:[\\-_.!~*'()a-zA-Z\\d$,;:@&=+]|%[a-fA-F\\d]{2})+) \\                (?# 6: registry)\n               )\n             |\n             (?!\\/\\/)) \\                          (?# XXX: '\\/\\/' is the mark for hostport)\n \\            (\\/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*(?:\\/(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*(?:;(?:[\\-_.!~*'()a-zA-Z\\d:@&=+$,]|%[a-fA-F\\d]{2})*)*)*)? \\                   (?# 7: path)\n           )(?:\\?((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\                (?# 8: query)\n        )\n        (?:\\#((?:[\\-_.!~*'()a-zA-Z\\d;\\/?:@&=+$,\\[\\]]|%[a-fA-F\\d]{2})*))? \\                 (?# 9: fragment)\n      \\s*\\z/x"
    from /usr/lib/ruby/1.9.1/syck/rubytypes.rb:284:in `yaml_new'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `transfer'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `node_import'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
    from /usr/lib/ruby/1.9.1/syck.rb:135:in `load'
    from /usr/lib/ruby/1.9.1/syck.rb:146:in `block in load_file'
    from /usr/lib/ruby/1.9.1/syck.rb:145:in `open'
    from /usr/lib/ruby/1.9.1/syck.rb:145:in `load_file'
    from (irb):428
    from /usr/bin/irb:12:in `<main>'

I've seen discussion about a patch to rubytypes.rb but otherwise nothing.

I'd rather not switch to Psych since that opens up another can of worms:

YAML::ENGINE.yamler = "psych"

# attempt to load from file again
TypeError: can't convert Fixnum into String

What's going on? Is there any hope? I had no prior knowledge of Syck nor Psych nor the internals of YAML parsing until this error made it non-transparent.

¿Fue útil?

Solución

You could store the regexp as string. When you read it back from the yaml file just convert the string back into a regexp. Regexp.new myregexp_str - This is of course just a workaround.

Update: Or you could do something that really stupid and patch psych to handle multiline regexp. Since the patched method is to long to post it grab it here. But here is the explanation of what was really patched.

module Psych
  module Visitors
    class ToRuby < Psych::Visitors::Visitor
       # ...
      def deserialize
        # ... L75
        when "!ruby/regexp"
        o.value =~ /^\/(.*)\/([mixn]*)$/m # <- notice the added "m" for multiline mode
        # ...
      end
    end
  end
end

using ruby-1.9.3-p194

Otros consejos

For everybody lurking around, this looks like an old bug. I just tried with ruby 1.9.3 and I'm getting this: [3] pry(main)> YAML.load(YAML.dump(/asd/m)) => /asd/m

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top