Ruby';s YAML只从大文件加载第一个记录,为什么?
好的,我有以下YAML文件,它是使用Rails的Ruby';s YAML只从大文件加载第一个记录,为什么?,ruby,parsing,yaml,Ruby,Parsing,Yaml,好的,我有以下YAML文件,它是使用Rails的YAML\u db生成的。这基本上是我的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
YAML\u db
生成的。这基本上是我的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"))
它只加载第一个管理员
,甚至实验
:
ruby-1.9.2-p0 > file.keys
=> ["admins"]
ruby-1.9.2-p0 > file["admins"]["records"].count
=> 1
为什么呢?我假设自动生成的.yml文件在语法上是正确的?当我运行rake:db:dump
和rake:db:load
时,它工作正常。三个连字符(--)分隔多个文档。请参阅以加载它们:
documents = YAML.load_stream(open("data-2011-01-27.yml")).documents
documents.map(&:keys)
#=> [["admins"], ["experiments"]]
指向YAML.load_流的链接不再工作。这是另一个文档。这只适用于Psych实现,这是唯一一个活着的实现。。。也许他们最终可以为ruby 2.x更新文档,因为1.8和1.9仍然支持
syck
实现。