require 'yaml' #全局变量配置文件目录及名称(yaml) ROLE_DIRECTORY = File.expand_path(RAILS_ROOT) + "/config/" GAME_GLOBAL_VARIABLE_FILE = ROLE_DIRECTORY + "application.yaml" # #用于创建全局变量配置config/roles/application.yaml # #<refresh_global_variable>重新创建全局变量文件</refresh_global_variable> def refresh_global_variable begin variables = { :product_change_second => params['product_change_second'], :show_second => params[:show_second], :job_second => params[:job_second], :bodhisattva_second => params[:bodhisattva_second], :beggar_second => params[:beggar_second], :event_min_second => params[:event_min_second], :event_max_second => params[:event_max_second], :continue_second => params[:continue_second], :humiliate_times => params[:humiliate_times], :beat_times => params[:beat_times] } File.open(GAME_GLOBAL_VARIABLE_FILE, 'w'){|f| YAML.dump(variables, f)} flash[:notice] = "全局变量文件创建完毕!" rescue => error flash[:notice] = "创建全局变量文件出现错误:" + error.message end redirect_to :action =>"index" end
生成后的文件内容是:
---
:bodhisattva_second: "4"
:beat_times: "0"
:beggar_second: "5"
:event_min_second: "6"
:product_change_second: "1"
:event_max_second: "7"
:show_second: "2"
:continue_second: "8"
:job_second: "3"
:humiliate_times: "9"
上面是我写的,但我要求生成后的内容应该是
bodhisattva_second: 4
beat_times: 0
beggar_second: 5
event_min_second: 6
product_change_second: 1
event_max_second: 7
show_second: 2
continue_second: 8
job_second: 3
humiliate_times: 9
不要冒号,不要双引号,也不要第一行的---
[quote]可要是加了to_i后,字符串参数就被强制转换了[/quote]
那字符串参数就不加 to_i 呗 ……
如果你无法确定是否该 to_i,在你贴的代码的第 11 行下面定义这么个内部函数:
maybe_to_i = proc{|s|s and (Integer(s) rescue s)}
然后将 hash 项改成:
'beat_times' => maybe_to_i[params[:beat_times]]
[quote]不要冒号,不要双引号,也不要第一行的---[/quote]
不要冒号,不要引号可以,但不要---,,,你确定你是要生成yaml吗?
[code="java"]variables = {
:continue_second=>"8",
:beggar_second=>"5",
:job_second=>"3",
:event_min_second=>"6",
:humiliate_times=>"9",
:product_change_second=>"1",
:event_max_second=>"7",
:bodhisattva_second=>"4",
:show_second=>"2",
:beat_times=>"0"
}
File.open("yourfile","w").each do |file|
variables.each{|key,value| file.write("#{key.to_s}:#{value.to_i}\n")"}
end
#yourfile:
continue_second:8
beggar_second:5
job_second:3
event_min_second:6
humiliate_times:9
product_change_second:1
event_max_second:7
bodhisattva_second:4
show_second:2
beat_times:0
[/code]
但这确实不是YAML...
楼上的不是 yaml …… yaml 冒号后面必须带一个空格的。
不要冒号和双引号,请把类似这样的:
[color=green]:beat_times => params[:beat_times][/color]
全部改成这样的:(key 改用字符串,value 改用整数)
[color=blue]'beat_times' => params[:beat_times].to_i [/color]
想去掉 --- 很简单,把:
[color=indigo]YAML.dump(variables, f)[/color]
改成:(简单的字符串替换)
[color=darkred]f.write YAML.dump(variables).sub("--- \n",'')[/color]
LZ为什么不要前面的---呢?
如果是想再YAML.load yaml_file前面的---根本没影响。
[code="ruby"]
class String
alias o_to_i to_i
def to_i
self =~/^\d+$/ ? self.o_to_i : self
end
end
variables = {
:continue_second=>"8",
:beggar_second=>"5",
:job_second=>"3",
:event_min_second=>"6",
:humiliate_times=>"9",
:product_change_second=>"1",
:event_max_second=>"7",
:bodhisattva_second=>"4",
:show_second=>"2",
:beat_times=>"0",
:fvck_lz =>"so bt"
}
File.open("yourfile.yml","w") do |file|
variables.each{|key,value| file.write("#{key.id2name}: #{value.to_i}\n")}
end
[/code]
[code="ruby"]
fvck_lz: so bt
product_change_second: 1
continue_second: 8
event_max_second: 7
beggar_second: 5
bodhisattva_second: 4
job_second: 3
show_second: 2
event_min_second: 6
beat_times: 0
humiliate_times: 9
[/code]