I need to open and read several files for a project. Some of them are plain text files, and I just want a standard File.open to read from. One of them is an XML file, and I want it automatically loaded into Nokogiri in the following way:xml = File.open(#{LOCATION}/#{filename}.xml){|f| Nokogiri::XML(f){|config| config.noblanks}}So I created a function that takes a pattern to select a file, and then optionally opens it with a block so that I can pass in the Nokogiri block above in the case of the xml file.How do I reduce the repetition in the code?# in read_xml...xml = file_from_pattern(XML_PATTERN){|f| Nokogiri::XML(f){|config| config.noblanks}}# in read_audit...audit_lines = file_from_pattern(AUDIT_PATTERN).readlinesdef file_from_pattern(pattern)  file_name = Dir.entries(#{LOCATION}).select{|f| f =~ pattern}[0]  if block_given?    yield File.open(#{LOCATION}/#{file_name})  else    File.open(#{LOCATION}/#{file_name})  endend
Use an optionally passed-in block without repeating yourself
ruby;lambda