Reputation: 53806
Am I comparing the pipe symbols |x| & |f| correctly below ?
|f| corresponds to the name of a dir |x| corresponds to the name of a file
Dir.foreach(@sortedFilesDir) do
|f|
@fileArray.each() {
|x|
if(x.match(/^#{f}/))
puts "match - "+x+","+f
end
}
end
Upvotes: 1
Views: 257
Reputation: 81510
I've made a few changes:
each
s with a map
and a find
."match - "+x+","+f
I haven't checked that it works, though.
directory_names = Dir.foreach(@sorted_files_dir)
matching_directory_names = @file_array.map do |filename|
directory_names.find do |directory_name|
filename.start_with?("/" + filename + "/")
end
end
@file_array.zip(matching_directory_names) do |filename, matching_directory_name|
next if matching_directory_name.nil?
puts "match - #{filename},#{matching_directory_name}"
end
I hope directory_names
doesn't get calculated every time the loop gets run! If it does, then you might want directory_names = Dir.foreach(@sorted_files_dir).to_a
at the start of the script.
Upvotes: 1
Reputation: 84150
It is the general convention that you use the do |var|
notation if your block is more than 1 line. If it is 1 line, use the {|var|}
syntax.
String interpolation is also preferred to concatenation as it uses less methods.
Another convention is that if your method doesn't take arguments, don't use the brackets at the end. (Like you have done in your each method)
Also, in the ruby world, we usually underscore_our_variable_names instead of camelCasing them. The exception being Constants (including class names) e.g. MyClass
or ThisIsOneOfMyConstants
Dir.foreach(@sorted_files) do |f|
@file_array.each do |x|
if(x.match(/^#{f}/))
puts "match - #{x},#{f}"
end
end
end
You are using them correctly if your goal is to list files in the specified directory that match an entry in your @file_array.
Upvotes: 1