我想指望紅寶石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不繼續推進要素在我的名單上。
你會如何區分'null'如果內容不是從null'的'有效的內容有效YAML。即如果YAML文檔是空的,或者只包含一個表示爲'null'的標量(即'〜','Null','null'和'NULL')? – Anthon