Answer the question
In order to leave comments, you need to log in
Why is there no TypeError (no implicit conversion of nil into String)?
There is this experimental code:
2.6.3 :058 > current_path = File.dirname(__FILE__)
=> "."
# пустая директория
2.6.3 :059 > file_paths = Dir.glob("#{current_path}/data/*.txt")
=> []
2.6.3 :060 > file_paths.each do |file_path|
2.6.3 :061 > if File.exist?(file_path)
2.6.3 :062?> puts "файл существует"
2.6.3 :063?> else
2.6.3 :064?> puts "файл не найден"
2.6.3 :065?> end
2.6.3 :066?> end
=> []
# в директории находится файл 1.txt
2.6.3 :067 > file_paths = Dir.glob("#{current_path}/data/*.txt")
=> ["./data/1.txt"]
2.6.3 :068 > file_paths.each do |file_path|
2.6.3 :069 > if File.exist?(file_path)
2.6.3 :070?> puts "файл существует"
2.6.3 :071?> else
2.6.3 :072?> puts "файл не найден"
2.6.3 :073?> end
2.6.3 :074?> end
файл существует
=> ["./data/1.txt"]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question