tokland's answer tokland's answer is fine, but I thought I'd chime in and suggest using Enumerable#each_with_object
instead of converting your data into an array and back.
def get_data(dir)
newest = get_most_recent_file_numbers(dir)
$file_prefixes.each_with_object({}) do |mapping, hash|
file = File.read("#{dir}/#{mapping.first}_#{newest[mapping.last]}
end
end
In this example, I'm just using each_with_object
to construct a new hash and populate it with values. You can also get rid of the need to call file = File.open ... file.readlines
by just using File.read
to get the same output.
tokland's answer is fine, but I thought I'd chime in and suggest using Enumerable#each_with_object
instead of converting your data into an array and back.
def get_data(dir)
newest = get_most_recent_file_numbers(dir)
$file_prefixes.each_with_object({}) do |mapping, hash|
file = File.read("#{dir}/#{mapping.first}_#{newest[mapping.last]}
end
end
In this example, I'm just using each_with_object
to construct a new hash and populate it with values. You can also get rid of the need to call file = File.open ... file.readlines
by just using File.read
to get the same output.
tokland's answer is fine, but I thought I'd chime in and suggest using Enumerable#each_with_object
instead of converting your data into an array and back.
def get_data(dir)
newest = get_most_recent_file_numbers(dir)
$file_prefixes.each_with_object({}) do |mapping, hash|
file = File.read("#{dir}/#{mapping.first}_#{newest[mapping.last]}
end
end
In this example, I'm just using each_with_object
to construct a new hash and populate it with values. You can also get rid of the need to call file = File.open ... file.readlines
by just using File.read
to get the same output.
tokland's answer is fine, but I thought I'd chime in and suggest using Enumerable#each_with_object
instead of converting your data into an array and back.
def get_data(dir)
newest = get_most_recent_file_numbers(dir)
$file_prefixes.each_with_object({}) do |mapping, hash|
file = File.read("#{dir}/#{mapping.first}_#{newest[mapping.last]}
end
end
In this example, I'm just using each_with_object
to construct a new hash and populate it with values. You can also get rid of the need to call file = File.open ... file.readlines
by just using File.read
to get the same output.