LoginSignup
16
19

More than 5 years have passed since last update.

Ruby のワンライナーで YAML の文法チェック

Last updated at Posted at 2016-04-20

やり方

単純に YAML.load(_file) で YAML を読み込む。

cat foo.yml | ruby -r yaml -e 'YAML.load STDIN'
# or
ruby -r yaml -e 'YAML.load_file "foo.yml"'

中身を表示したければ、ruby -r yaml -e 'p YAML.load_file "foo.yml"' のように、p を付ければ良い。

壊れた YAML の場合

% broken.yml
foo: bar
bar baz:quz
% ruby -r yaml -e 'p YAML.load_file "broken.yml"'
/home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:377:in `parse': (broken.yml): could not find expected ':' while scanning a simple key at line 2 column 1 (Psych::SyntaxError)
        from /home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:377:in `parse_stream'
        from /home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:325:in `parse'
        from /home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:252:in `load'
        from /home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:471:in `block in load_file'
        from /home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:471:in `open'
        from /home/key-amb/.rbenv/versions/2.3.0/lib/ruby/2.3.0/psych.rb:471:in `load_file'

…というわけで、とりあえずこれでよさそう。

参考

16
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
19