def Object.const_missing(name)
@looked_for ||= {}
str_name = name.to_s
raise "Class not found: #{name}" if @looked_for[str_name]
@looked_for[str_name] = 1
file = str_name.downcase
require file
klass = const_get(name)
return klass if klass
raise "Class not found: #{name}"
end
Now I understand that the point of this is to rescue from const_missing when autoloading is triggered and a Constant cannot be located within its current nesting scope. I also understand the mission is to require a file that perhaps got overlooked in the autoloading process, whose filename is the same name of the missing constant.
However, there are a few things that confuse me. Firstly, @looked_for ||= {}
which if I'm not mistaken is saying @looked_for || @looked_for = {}
. Why did they decide to place it there, considering we know @looked_for is nil? We are just creating that instance variable and we KNOW that its going to be empty so why not just say @looked_for = {}
?
Also, it says if @looked_for[str_name]
but how could our @looked_for hash have an existing key already if we are just now creating it? Won't it be nil? What's even more, is that the entire rest of the function lies under that if
statement so nothing happens after the fact, if @looked_for is indeed empty.
In the event that this @looked_for variable somehow already existed and did have that key of our constant name, @looked_for[str_name] why are we setting it to 1
? What is the purpose of that?