The code below, config.yml and testvars.rb are supposed to created some
arrays and hashes.
When I assign the directory1 and directory2 hashes directly into
dirArray :
dirname.each { | k | dirArray = k }
dirArray doesn't contains the keys "directory1" and "directory2", which
is what I expect
but when I assign them via key = value
dirname.each { | k | k.each {| l, m | dirArray[l]=m } }
dirArray contains the keys "directory1" and "directory2"
I know my grasp of ruby semantics is not up to scratch yet, but is it a
YAML issue or a Ruby issue?
config4.yml
···
=============
directories :
directory1 :
directory : /thisdirectory/yada/yada/yada
archivename : archivename01
configdir : configdir01
workdir : workingdir01
logsdir : logsdir01
directory2 :
directory : /thisdirectory/yada2/yada2/yada2
archivename : archivename02
configdir : configdir02
workdir : workingdir02
logsdir : logsdir02
Code - testvars4.rb
require 'yaml'
config = YAML::load(File.open('config4.yml'))
puts "assign via hash directly\n"
puts ""
dirArray = Hash.new
config["directories"].each { | dirname |
puts ""
dirname.each { | k | dirArray = k }
puts "dirArray elements start = \n"
puts ""
dirArray.each { | l, m | puts "#{l} => #{m}" }
puts ""
puts "dirArray elements end= \n"
}
puts ""
puts "assign via | key = val | \n"
puts
dirArray = Hash.new
config["directories"].each { | dirname |
dirname.each { | k | k.each {| l, m | dirArray[l]=m } }
puts "dirArray elements start = \n"
puts ""
dirArray.each { | l, m | puts "#{l} => #{m}" }
puts ""
puts "dirArray elements end= \n"
puts ""
}
Output
assign via hash directly
dirArray elements start =
directory => /thisdirectory/yada/yada/yada
configdir => configdir01
workdir => workingdir01
archivename => archivename01
logsdir => logsdir01
dirArray elements end=
dirArray elements start =
directory => /thisdirectory/yada2/yada2/yada2
configdir => configdir02
workdir => workingdir02
archivename => archivename02
logsdir => logsdir02
dirArray elements end=
assign via | key = val |
dirArray elements start =
directory => /thisdirectory/yada/yada/yada
configdir => configdir01
archivename => archivename01
workdir => workingdir01
logsdir => logsdir01
directory1 =>
dirArray elements end=
dirArray elements start =
directory => /thisdirectory/yada2/yada2/yada2
configdir => configdir02
archivename => archivename02
workdir => workingdir02
logsdir => logsdir02
directory1 =>
directory2 =>
dirArray elements end=
--
Posted via http://www.ruby-forum.com/.