There are tons of solutions here.
Returning a double value.
def self.foobar(docfiles)
temporary_file_paths = []
temporary_file_names = []
docfiles.each do |docfile|
if File.exist? docfile.path
temporary_file_paths << new_path
temporary_file_names << something_else
end
end
[temporary_file_paths, temporary_file_names]
end
paths, names = Class.foo(...)
Using collect.
def self.foobar(docfiles)
docfiles.map do |docfile|
File.exist?(docfile.path) ? [new_path, something_else] : nil
end.compact
end
paths, names = Class.foo(...)
Using inject (if you want a hash)
def self.foobar(docfiles)
docfiles.inject({ :file_paths => [], :file_names => []}) do |all, docfile|
if File.exist?(docfile.path)
all[:file_paths] << new_path
all[:file_names] << something_else
end
all
end
end
All the solutions above don't change the main method logic.
I don't like very much using arrays/hashes instead of objects so I usually end up converting hashes in objects when the execution requires multiple elaborations.
TemporaryFile = Struct.new(:path, :something_else)
def self.foobar docfiles
docfiles.map do |docfile|
if File.exist?(docfile.path)
TemporaryFile.new(new_path, something_else)
end
end.compact
end
Also, I don't know the meaning of something
else but if it's something you can get from the new_path, then you can use lazy execution.
TemporaryFile = Struct.new(:path) do
def something_else
# ...
end
end
def self.foobar docfiles
docfiles.map do |docfile|
TemporaryFile.new(new_path) if File.exist?(docfile.path)
end.compact
end