Ruby で自家製スクリプトを書く 07

YAML を読み込んだ実行結果を表示させる。

config.yaml

encoding:
  output: UTF-8
  input: Shift-JIS
csv:
  path: sample.csv
  # 「,」の後ろに半角空白が入力されている。
  # シングルクォーテーションで囲まないとエラーが出るため、
  # シングルクォーテーションを記述している。
  fs: ', '
  # ダブルクォーテーションも同様
  quote: '"'
  headers: false

read_yaml.rb

#!/usr/bin/env ruby
$KCODE='utf8'

require 'yaml'

config_file = 'config.yaml'

configs = YAML.load_file(config_file)
p configs

実行結果。

% ./read_yaml.rb
{"encoding"=>{"output"=>"UTF-8", "input"=>"Shift-JIS"}, "csv"=>{"fs"=>", ", "quote"=>" headers: false", "path"=>"sample.csv"}}