So I’ve been working with Dir class lately, and this is what I came up with. I would like to share it and see how others would use this code.
def find_file(file_name)
usr_dir = Dir.glob("#{Dir.home}/**/")
usr_dir.each_with_index do | value , index|
file = File.exists?("#{value}#{file_name}")
if file == true
puts value + filename
end
end
end
Of course this is just the function, it can use some error handling. All in all, basically what this is doing is globbing all user sub directories inside a Linux machine. The glob stores it’s data inside of an array.
After the glob is ran, I set up a block with the .each_with_index method on the glob array, and add the find_file argument, this case a file name, to the glob array values.
To do this I’m using the File.exists?() Method. The block code runs through the glob array values in this method, which returns true or false.
With my if statement, when File.exists? Does get a true statement, output that value with the file name.
=>find_file “test.rb”
=> /home/username/Documents/ruby/test.rb
I took it a step further. Knowing I can pull files I wanted to find as well as thier full dir path, I tried requiring some projects I have that I normally have to load inside that pwd.
require “#{find_file ‘test.rb’}”
=>True