Ruby的YAML只从大文件加载第一个记录,为什么?

时间:2011-01-27 11:31:59

标签: ruby parsing yaml

好的,我有使用yaml_db为Rails生成的以下YAML文件。所以这基本上是我的Rails数据库的自动生成导出:

--- 
admins: 
  columns: 
  - id
  - username
  - email
  - encrypted_password
  - password_salt
  - sign_in_count
  - current_sign_in_at
  - last_sign_in_at
  - current_sign_in_ip
  - last_sign_in_ip
  - failed_attempts
  - unlock_token
  - locked_at
  - created_at
  - updated_at
  records: 
  - - 1
    - 
    - lalala@lalala.at
    - $2a$10$dZU50HD6paWS7EjKuWAruOFdwt9eqxiNTRh/D4sj8cqSzy5gjYd2i
    - $2a$10$dZU50HD6paWS7EjKuWAruO
    - 86
    - 2011-01-27 07:37:45 Z
    - 2011-01-26 13:27:13 Z
    - 12.34.56.78
    - 12.34.56.78
    - 0
    - 
    - 
    - 2010-12-23 09:20:46 Z
    - 2011-01-27 07:37:45 Z
  - - 2
    - admin
    - lalalalala@lalalala.com
    - $2a$10$3DML64hdCCvG90bnhIpN/unEEm6C.a9FqGrAFlFHU0.2D54DSQ1Ni
    - $2a$10$3DML64hdCCvG90bnhIpN/u
    - 1
    - 2011-01-21 09:52:14 Z
    - 2011-01-21 09:52:14 Z
    - 12.34.56.78
    - 12.34.56.78
    - 0
    - 
    - 
    - 2011-01-05 14:29:49 Z
    - 2011-01-21 09:52:14 Z

--- 
experiments: 
  columns: 
  - id
  - description
  - startdate
  - enddate
  - maps_base_URI
  - maps_count
  - queries_count
  - proposals_count
  - created_at
  - updated_at
.......

现在,当我尝试使用:

在Ruby中加载此YAML文件时
file = YAML.load(File.open("data-2011-01-27.yml"))

加载的次数不超过第一个admin,甚至不加experiments

ruby-1.9.2-p0 > file.keys
 => ["admins"]

ruby-1.9.2-p0 > file["admins"]["records"].count
 => 1 

为什么?我会假设自动生成的.yml文件在语法上是正确的吗?当我运行rake:db:dumprake:db:load时,它运行正常。

1 个答案:

答案 0 :(得分:11)

三个连字符( --- )分隔多个文档。请参阅YAML.load_stream以加载所有内容:

documents = YAML.load_stream(open("data-2011-01-27.yml")).documents
documents.map(&:keys)
#=> [["admins"], ["experiments"]]