Use an optionally passed-in block without repeating yourself
Some notes:


select {... }[0] -> detect { ... }.
Is LOCATION a string? Then, "#{LOCATION}" -> LOCATION
Use File#join instead of manual string manipulation.
The only repetition in your code is File.open("#{LOCATION}/#{file_name}"). The solution is very simple: create a local variable and use it.
Don't you check whether file_name is nil?
File#open accepts a block, why don't you simply pass it? you get the file descriptor closed for free in the block case.


I'd write:

def file_from_pattern(pattern, &amp;block)
  file_name = Dir.entries(LOCATION).detect { |f| f =~ pattern }
  if file_name
    # Refactor 1:
    # fd = File.open(File.join(LOCATION, file_name))
    # block_given? ? yield(fd) : fd
    File.open(File.join(LOCATION, file_name), &amp;block)
  end
end