Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby's YAML only loads first Records from a large File, why?

Tags:

parsing

ruby

yaml

Okay, I have the following YAML file that was generated by using yaml_db for Rails. So this is basically an autogenerated export of my Rails database:

--- 
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
    - 
    - [email protected]
    - $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
    - [email protected]
    - $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
.......

Now when I try to load this YAML file in Ruby with:

file = YAML.load(File.open("data-2011-01-27.yml"))

It doesn't load more than the first admin, not even the experiments:

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

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

Why is that? I would assume that the autogenerated .yml file is syntactically correct? When I run rake:db:dump and rake:db:load it works just fine.

like image 515
slhck Avatar asked Dec 22 '22 18:12

slhck


1 Answers

Three hyphens (---) separate multiple documents. See YAML.load_stream to load them all:

documents = YAML.load_stream(open("data-2011-01-27.yml")).documents
documents.map(&:keys)
#=> [["admins"], ["experiments"]]
like image 122
tokland Avatar answered Feb 19 '23 06:02

tokland