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
不要冒号,不要双引号,也不要第一行的---
问题补充:
传入的参数如果是单纯的数字,用to_i是可以实现的。但关键是我传入的参数不一定是单单的数字,加入我传入字符串,那样实现输出是没有双引号的。可要是加了to_i后,字符串参数就被强制转换了!