Get the keyname from a YAML configuration file

Viewed 46

I would like to get the name of a key in a YAML configuration file like the following The YAML file contains a collection of structs in structs, the organisations have a number represented as a symbol which is the key/class name. I need to retrieve this symbol

require 'yaml'
data = YAML.load(DATA)
data.organisations.each do |organisation|
  organisation #<struct language="nl", name="myname">
  # following line is what I need, I expect it to be :"121"
  organisation.class #<Class:0x00000004fd4248>
end
__END__
--- !ruby/struct
organisations: !ruby/struct
  :121: !ruby/struct
    language: nl
    name: organisationname

Can someone help me retrieving the name of the struct ?

1 Answers

I tried around a bit and found this:

data.organisations.members
=> [:"121"]

The way I found this (which is useful in other circumstances, too) is the following:

data.organisations.methods - Object.new.methods

And then I tried out every method that seemed reasonable.

Related