O Reilly Ruby Cookbook source code

源代码在线查看: 11 - reading and writing configuration files.rb

软件大小: 292 K
上传用户: lz0324
关键词: Cookbook Reilly source Ruby
下载地址: 免注册下载 普通下载 VIP

相关代码

				require 'yaml'				configuration = { 'color' => 'blue',				                  'font' => 'Septimus',				                  'font-size'  => 7 }				open('text.cfg', 'w') { |f| YAML.dump(configuration, f) }								open('text.cfg') { |f| puts f.read }				# --- 				# font-size: 7				# color: blue				# font: Septimus								open('text.cfg') { |f| YAML.load(f) }				# => {"font-size"=>7, "color"=>"blue", "font"=>"Septimus"}				#---				configuration = [ { 'name' => 'Alice', 'donation' => 50 },				                  { 'name' => 'Bob', 'donation' => 15, 'currency' => "EUR" } ]				open('donors.cfg', 'w') { |f| YAML.dump(configuration, f) }				open('donors.cfg') { |f| puts f.read }				# ---				# - name: Alice				#   donation: 50				# - name: Bob				#   donation: 15				#   currency: EUR				#---				puts ({ 'measurements' => 'metric' }.to_yaml)				# --- 				# measurements: metric								puts ({ :measurements => :metric }.to_yaml)				# --- 				# :measurements: :metric				#---							

相关资源