关于序列化的奇怪问题,可能是我做错了什么,但不确定是什么。
提前感谢你的帮助。
输入yaml文档
---
ssl_user_clients:
- first_name: donnald
surname: duck
email: donnald.duck@acme.corp
country: fantasyland
state: desert
locality: Disney
org_name: Acme corp预期的yaml文档
---
ssl_user_clients:
- first_name: donnald
surname: duck
password: k)NzzC+&Dg?-RY|0
email: donnald.duck@acme.corp
country: fantasyland
state: desert
locality: Disney
org_name: Acme corp我的代码出现了奇怪的结果:
---
ssl_user_clients:
- &1
first_name: donnald
surname: duck
email: donnald.duck@acme.corp
country: fantasyland
state: desert
locality: Disney
org_name: Acme corp
*1:
first_name: donnald
surname: duck
email: donnald.duck@acme.corp
country: fantasyland
state: desert
locality: Disney
org_name: Acme corp
password: k)NzzC+&Dg?-RY|0我的Ruby代码:
#!/usr/bin/env ruby
require "yaml"
def generate_activation_code(size = 16)
charset = %w{0 1 2 3 4 6 7 9 A C D E F G H J K M N P Q R T V W X Y Z a b c d e f g h i j k m n o p q r s t u v w x y z ! @ $ % ^ & * ( ) _ ? ~ + - = / \ | < > { } [ ]}
(0...size).map{ charset.to_a[rand(charset.size)] }.join
end
def get_cleaned_password
password = generate_activation_code
password.split().collect{|x| x.strip}.join()
end
hash = YAML.load(File.read(ARGV[0]))
puts "Hash from file"
puts hash
puts
puts
for user in hash["ssl_user_clients"]
if not user["password"]
new_password = get_cleaned_password
end
pass = { "password" => new_password }
hash[user] = user.merge pass
end
puts hash.to_yaml
open(ARGV[0], File::TRUNC) {}
File.open(ARGV[0], 'w') {|f| f.write hash.to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true ) }请有人协助这一点,因为这让我困惑,我真的没有想到会看到这一点。
一些调试信息。
Hash from file
{"ssl_user_clients"=>[{"first_name"=>"donnald", "surname"=>"duck", "email"=>"donnald.duck@acme.corp", "country"=>"fantasyland", "state"=>"desert", "locality"=>"Disney", "org_name"=>"Acme corp"}]}
Hash before hash.to_yaml
{"ssl_user_clients"=>[{"first_name"=>"donnald",
"surname"=>"duck",
"email"=>"donnald.duck@acme.corp",
"country"=>"fantasyland",
"state"=>"desert",
"locality"=>"Disney",
"org_name"=>"Acme corp"}],
{"first_name"=>"donnald",
"surname"=>"duck",
"email"=>"donnald.duck@acme.corp",
"country"=>"fantasyland",
"state"=>"desert",
"locality"=>"Disney",
"org_name"=>"Acme corp"}=>{"first_name"=>"donnald",
"surname"=>"duck",
"email"=>"donnald.duck@acme.corp",
"country"=>"fantasyland",
"state"=>"desert",
"locality"=>"Disney",
"org_name"=>"Acme corp",
"password"=>"4?zkoff3^hmMC-<7"}}下面详细介绍的更改修复了yaml的损坏,但是输出有缩进问题。
很棒的东西,这是预期的工作,但是输出有一个缩进问题。
下面修复了根据@infused (谢谢)给出的建议的损坏:
for user in hash["ssl_user_clients"]
if not user["password"]
user["password"] = get_cleaned_password
end
end按照预期更正输出。
---
ssl_user_clients:
- first_name: donnald
surname: duck
email: donnald.duck@acme.corp
country: fantasyland
state: desert
locality: Disney
org_name: Acme corp
password: =)%DoHHpyRrx|?v=我假设下面的代码可以确保缩进是正确的。
to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true ) }发布于 2015-04-30 00:54:56
您正在破坏for user in hash["ssl_user_clients"]循环中的散列,因为user包含一个散列,并且您正在使用user作为键创建一个新元素。
通过以下方式更新密码:
for user in hash["ssl_user_clients"]
user['password'] = get_cleaned_password
end现在,您可以使用puts hash.to_yaml验证,hash将更新每个用户的密码。
https://stackoverflow.com/questions/29948598
复制相似问题