I need some help clarifying issues with YAML and hashes


#1

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=


#2

On 6/26/07, Frank C. removed_email_address@domain.invalid wrote:

The code below, config.yml and testvars.rb are supposed to created some
arrays and hashes.

Your YAML is all hashes; there are no arrays.

When I assign the directory1 and directory2 hashes directly into
dirArray :

dirname.each { | k | dirArray = k }

dirname is a Hash, with two entries that are themselves hashes. When
you call each() on a Hash, you get a 2-element (key, value) array. So
k is an array, not a hash.

What are you trying to do?

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”

Yes, given what k is.

I know my grasp of ruby semantics is not up to scratch yet, but is it a
YAML issue or a Ruby issue?

I’m guessing it’s related to your misconception of what the YAML is
actually representing.

Use some calls to p() to dump some of these structures so you can see
what you’re dealing with.