2017-06-15 458 views
0

我想指望红宝石YAML module返回空的YAML.load_file(foo)如果foo不是YAML文件。但我得到异常:知道,如果文件是YAML或不

did not find expected alphabetic or numeric character while scanning an alias at line 3 column 3 (Psych::SyntaxError) 
    from /usr/lib/ruby/2.4.0/psych.rb:377:in `parse_stream' 
    from /usr/lib/ruby/2.4.0/psych.rb:325:in `parse' 
    from /usr/lib/ruby/2.4.0/psych.rb:252:in `load' 
    from /usr/lib/ruby/2.4.0/psych.rb:473:in `block in load_file' 
    from /usr/lib/ruby/2.4.0/psych.rb:472:in `open' 
    from /usr/lib/ruby/2.4.0/psych.rb:472:in `load_file' 
    from ./select.rb:27:in `block in selecting' 
    from ./select.rb:26:in `each' 
    from ./select.rb:26:in `selecting' 
    from ./select.rb:47:in `block (2 levels) in <main>' 
    from ./select.rb:46:in `each' 
    from ./select.rb:46:in `block in <main>' 
    from ./select.rb:44:in `each' 
    from ./select.rb:44:in `<main>' 

我如何类选一个文件是一个YAML文件或并非没有例外?就我而言,我浏览到一个目录和流程降价文件:我添加到列表降价文件,用钥匙output: word,我返回列表

mylist = Array.new 
mylist = [] 
for d in (directory - excludinglist) 
begin 
    info = YAML.load_file(d) 
    if info 
    if info.has_key?('output') 
     if info['output'].has_key?(word) 
     mylist.push(d) 
     end 
    end 
    end 
rescue Psych::SyntaxError => error 
    return [] 
end 
end 
return mylist 

当我赶上异常,bucle不继续推进要素在我的名单上。

+0

你会如何区分'null'如果内容不是从null'的'有效的内容有效YAML。即如果YAML文档是空的,或者只包含一个表示为'null'的标量(即'〜','Null','null'和'NULL')? – Anthon

回答

2

答案很简单:你不能。

因为YAML只是一个文本文件,要知道一个给定的文本文件是否是YAML与否的唯一方法是分析它。解析器将尝试解析文件,如果它不是有效的YAML,它会引发错误。

错误和异常红宝石的公共部分,尤其是在IO的世界。没有理由害怕他们。您可以轻松地从他们救出,并继续用自己的方式:

begin 
    yaml = YAML.load_file(foo) 
rescue Psych::SyntaxError => e 
    # handle the bad YAML here 
end 

你提到下面的代码将无法正常工作,因为你需要处理在一个目录多个文件:

def foo 
    mylist = [] 
    for d in (directory - excludinglist) 
    begin 
     info = YAML.load_file(d) 
     if info 
     if info.has_key?('output') 
      if info['output'].has_key?(word) 
      mylist.push(d) 
      end 
     end 
     end 
    rescue Psych::SyntaxError => error 
     return [] 
    end 
    return mylist 
end 

唯一的问题在这里当你遇到错误时,你会尽早从函数中返回。如果你不回,for循环将继续下去,你会得到你想要的功能:

def foo 
    mylist = [] 
    for d in (directory - excludinglist) 
    begin 
     info = YAML.load_file(d) 
     if info 
     if info.has_key?('output') 
      if info['output'].has_key?(word) 
      mylist.push(d) 
      end 
     end 
     end 
    rescue Psych::SyntaxError => error 
     # do nothing! 
     # puts "or your could display an error message!" 
    end 
    end 
    return mylist 
end 
+0

如果我这样做,那么我无法处理目录中的所有文档。我的代码看起来像这样' MYLIST = Array.new \t MYLIST = [] \t在d(目录 - excludinglist) \t \t开始 \t \t \t信息= YAML.load_file(d) \t \t \t如果信息 \t \t \t \t如果info.has_key?( '输出') \t \t \t \t \t如果信息[ '输出']。公顷s_key?(word) \t \t \t \t \t \t mylist。推(d) \t \t \t \t \t端 \t \t \t \t端 \t \t \t端 \t \t救援精极度紧张::的SyntaxError =>错误 \t \t \t返回[] \t \t端 \t端 \t返回mylist' – somenxavier

+0

@some nxavier你可以编辑你的原始问题,包括代码示例,格式正确吗? – eiko

+0

完成。现在你能为我的问题提供某种解决方案吗?感谢 – somenxavier

2

Psych::SyntaxError得到了Psych::Parser#parse的提高,其源代码是用C编写的。所以除非你想使用C,否则你不能在Ruby中为该方法编写补丁来防止引发异常。

不过,你当然可以救例外,像这样:

begin 
    foo = YAML.load_file("not_yaml.txt") 
rescue Psych::SyntaxError => error 
    puts "bad yaml" 
end 
相关问题