68
60

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

YAML入門

Last updated at Posted at 2017-07-18

公式:http://yaml.org/

主にdotinstall YAML入門のまとめ

実際の使用例はこちら

シーケンス 配列

フロースタイル
[red,blue,green]
ブロックスタイル
- red
- blue
- green
入れ子
- red
- blue
- # ここは空白
  - orange
  - pink
- green

マッピング ハッシュ(key/value)

{site_name : site name, description: site description}
# :で位置を揃えると美しい
site_name  : site name
description: site description
入れ子
meta:
  title: page title
  description: description

組み合わせ

配列をもつハッシュ

keywords: [keyword01, keyword02, keyword03]

ハッシュの配列

- name: sato
  age : 20
- name: suzuki
  age : 25
インライン
- { name: sato, age: 20 }
- { name: suzuki, age: 25 }

改行

最後の改行を取り除く

|-
  this
  is
  a
  pen.

最後の改行を取り除かない

|+
  this
  is
  a
  pen.

アンカー、エイリアス

&: アンカー(名前をつける)
*: エイリアス(参照する)

- &taguchi
  name: taguchi
  score: 80
- &fkoji
  name: fkoji
  score: 90
  friends:
    - *taguchi
- name: dotinstall
  score: 100
  friends:
    - *taguchi
    - *fkoji

ハッシュのマージ

defaults: &defaults
  user: dbuser
  password: dbpassword
  database: myapp

development:
  <<: *defaults # マージ
  database: myapp_dev

test:
  <<: *defaults
  database: myapp_test

production:
  <<: *defaults
  database: myapp_prod

YAMLでの書き方がわからないとき

.to_yamlメソッドを使う

users = [
  {'name' => 'sato', 'age' => 20},
  {'name' => 'suzuki', 'age' => 30},
]

puts users.to_yaml
結果(YAMLでの表記がわかる)
- name: sato
  age: 20
- name: suzuki
  age: 30

参考
プログラマーのための YAML 入門 (初級編)

68
60
0

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
68
60

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?