本文簡要介紹ruby語言中 Psych.parse
的用法。
用法
parse(yaml, filename: nil)
解析 yaml
中的 YAML
字符串。返回 Psych::Nodes::Document
。如果引發 Psych::SyntaxError
,則在異常消息中使用filename
。
當檢測到 YAML
語法錯誤時引發 Psych::SyntaxError
。
例子:
Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>
begin
Psych.parse("--- `", filename: "file.txt")
rescue Psych::SyntaxError => ex
ex.file # => 'file.txt'
ex.message # => "(file.txt): found character that cannot start any token"
end
有關 YAML
AST 的更多信息,請參閱 Psych::Nodes
。
相關用法
- Ruby Psych.parse_stream用法及代碼示例
- Ruby Psych.unsafe_load用法及代碼示例
- Ruby Psych.dump用法及代碼示例
- Ruby Psych.dump_stream用法及代碼示例
- Ruby Psych.safe_load用法及代碼示例
- Ruby Psych.load_stream用法及代碼示例
- Ruby Psych.safe_dump用法及代碼示例
- Ruby Psych模塊用法及代碼示例
- Ruby PrettyPrint.current_group用法及代碼示例
- Ruby Pathname.<=>用法及代碼示例
- Ruby Pathname.children用法及代碼示例
- Ruby Process.groups用法及代碼示例
- Ruby Process.wait2用法及代碼示例
- Ruby Process.getpgrp用法及代碼示例
- Ruby Proc.eql?用法及代碼示例
- Ruby PTY.open用法及代碼示例
- Ruby PrettyPrint.genspace用法及代碼示例
- Ruby PKey.sign_raw用法及代碼示例
- Ruby Profiler模塊用法及代碼示例
- Ruby Process.setproctitle用法及代碼示例
- Ruby PPMethods.comma_breakable用法及代碼示例
- Ruby Process.setrlimit用法及代碼示例
- Ruby Proc.prc ==用法及代碼示例
- Ruby Profiler.raw_data用法及代碼示例
- Ruby Process.uid用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Psych.parse。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。