Forum: Ruby nested hash / openstruct question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pugal P (Guest)
on 2007-04-02 02:55
Hello,
I am a newbie to Ruby. I am having trouble reading the entries from
nested yaml configuration file. Any help would be highly appreciated.

YAML configuration file:
---
AppName: Record Center
DEV:
- user: test
  webServers:
    webserver1: wserver1
    webserver2: wserver2
  appServers:
    appserver1: aserver3
    appserver2: aserver4
  password: blah
QA:
- user: test
  webServers:
    webserver1: wserver11
    webserver2: wserver22
  appServers:
    appserver1: aserver33
    appserver2: aserver44
  password: blah

My code uses hash to openstruct code
(http://www.dribin.org/dave/blog/archives/2006/11/1...).
Things work fine when I read the AppName. When I try to read the user  /
password or webServers or appServers entry... I am getting the following
exception.
5c706e7176ace1aa3748ce3be8f80f0c?d=identicon&s=25 Pugal P. (pugalp)
on 2007-04-02 02:58
Sorry...I hit the submit button before pasting the actual error message.
recordcentercrawler/rccrawler.rb:30: undefined method `webServers' for
#<Array:0
x2dbc868> (NoMethodError)

Any help would be highly appreciated.

Thanks
Pugal

Pugal P wrote:
> Hello,
> I am a newbie to Ruby. I am having trouble reading the entries from
> nested yaml configuration file. Any help would be highly appreciated.
>
> YAML configuration file:
> ---
> AppName: Record Center
> DEV:
> - user: test
>   webServers:
>     webserver1: wserver1
>     webserver2: wserver2
>   appServers:
>     appserver1: aserver3
>     appserver2: aserver4
>   password: blah
> QA:
> - user: test
>   webServers:
>     webserver1: wserver11
>     webserver2: wserver22
>   appServers:
>     appserver1: aserver33
>     appserver2: aserver44
>   password: blah
>
> My code uses hash to openstruct code
> (http://www.dribin.org/dave/blog/archives/2006/11/1...).
> Things work fine when I read the AppName. When I try to read the user  /
> password or webServers or appServers entry... I am getting the following
> exception.
Nobuyoshi Nakada (Guest)
on 2007-04-02 07:58
(Received via mailing list)
Hi,

At Mon, 2 Apr 2007 09:58:45 +0900,
Pugal Panneerselvam wrote in [ruby-talk:246323]:
> Sorry...I hit the submit button before pasting the actual error message.
> recordcentercrawler/rccrawler.rb:30: undefined method `webServers' for
> #<Array:0 x2dbc868> (NoMethodError)

Your YAML has arrays under DEV and QA.

> > DEV:
> > - user: test

Try replacing "-" with " ".
This topic is locked and can not be replied to.