P
P
Pirozhok_s_pavidlom2019-08-15 11:57:47
ruby
Pirozhok_s_pavidlom, 2019-08-15 11:57:47

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"]

Question: why on line 61 when the exist method? class File iterator each passes nil , then there is no TypeError (no implicit conversion of nil into String) ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question