I have a simple YAML array with values like this
%YAML 1.1
---
-
- 'd7744c3878'
- '80705686'
-
- 'c349b086b1'
- '80705686'
-
- '516e25d139'
- '95203563'
I try the following code to iterate on them and add them to the database.
YAML.load_file("db/tokens.yml").each_value do |yml_token|
token = Token.find_or_create_by_token_origin(
token_origin: yml_token[0],
token_value: yml_token[1])
end
But I get a NoMethodError: undefined method each_value for #<String:0x000000059ab7f8> error when try to run the code.
Any idea what is causing this?
UPDATE:
Using Psych.load as suggested below I also get a SyntaxError: db/gallery_tokens.yml:1: unknown type of %string error.
%YAML 1.1
require "psych"
Psych.load("db/tokens.yml").each do |yml_token|
token = Token.find_or_create_by_token_origin(
token_origin: yml_token[0],
token_value: yml_token[1])
end
It’s likely that the declaration at the top, “%YAML 1.1”, is causing problems.
No error:
Error:
Here I’m using ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-darwin9.8.0]. I also note that in an unmodified environment,
#each_valueis available for aHashbut not anArray(@oldergod).It looks like Psych, which is the default YAML interepreter in later versions of Ruby, can handle the
%YAML 1.1directive:Possible alternatives to using Psych directly would be to switch to a later version of Ruby or to strip the
%YAML 1.1header from the file.