From: Gennady Bystritsky Date: 2006-05-11T15:18:55+09:00 Subject: Re: A more elegant way to do this? Array() will do the job: Array(nil) => [] Array([1,2,3]) => [1, 2, 3] Array(1) => [1] In your case: Array(conf['hosts'][hostname]['groups']).each { ... } Gennady. > -----Original Message----- > From: Tim Uckun [mailto:timuckun@gmail.com] > Sent: Wednesday, May 10, 2006 21:32 > To: ruby-talk ML > Subject: A more elegant way to do this? > > Can anybody suggest a more elegant and rubyish way to do this? > > I am reading a YAML file and then trying to iterate over arrays and > hashes that may or may not be there. > > unless conf['hosts'][hostname]['groups'].nil? > conf['hosts'][hostname]['groups'].each { |group| > unless conf['crontables'][group].nil? > conf['crontables'][group].each { | tab_item| > tab_entries << tab_item > } > end #Unless > } > end #unless >