From: Joel VanderWerf Date: 2006-05-12T15:50:38+09:00 Subject: Re: A more elegant way to do this? Tim Uckun wrote: > 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 A slight variation on the other suggestions: def each_element_of(enum) enum and enum.each {|elt| yield elt} end each_element_of conf['hosts'][hostname]['groups'] do |group| each_element_of conf['crontables'][group] do |tab_item| tab_entries << tab_item end end Another slight variation: conf['hosts'][hostname]['groups'].to_a.each do |group| conf['crontables'][group].to_a.each do | tab_item| tab_entries << tab_item end end -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407