I have a following YAML file:
---
main-menu:
- item: Test1
param: value
- item: Test2
param: value
- notitem: Test3
param: value
I’m trying to parse all item entries like so:
#Load menu file
menu = YAML.load_file(file)
#Recursive parse function
parse = lambda do |node|
node.each do |item|
if !item['item'].nil? then
.. do something with item ..
end #end if
end #end each
end #end parse
#Return the main menu
parse.call(menu['main-menu'])
Which works just fine… Is there an easier way to filter out all entries that aren’t an item using node.each?
Not that I can see.
nodeis anArrayofHashobjects, so each needs to be conditionally checked before you can.. do something with item ... You could just clean up thenode.eachslightly with anextYou could alternatively call
rejectwith a blockThough I don’t know that this is really “easier”!