0
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?

  • Possible duplicate of [What does ||= (or-equals) mean in Ruby?](https://stackoverflow.com/questions/995593/what-does-or-equals-mean-in-ruby) – jonrsharpe Oct 31 '17 at 14:53
  • The main vice of my question would be, what's the purpose of setting @looked_for[str_name] = 1. – rackymortor Oct 31 '17 at 15:09
  • 2
    You're missing that this method may be invoked multiple times. We **don't** "know @looked_for is nil" at all. It *won't* be, except on the first invocation. – user229044 Oct 31 '17 at 15:13
  • Also @sergio to slightly nitpick, `x ||= y` is **not** equivalent to `x = x || y`. A lot has been written about the subtle differences, but unlike `a += b` and `a = a + b`, their implementation is completely different. – user229044 Oct 31 '17 at 15:20
  • @meagar: if you can see deleted comments, I was asserting that `x ||= y` is equivalent to `x || x = y` (in functionality, if not implementation). Off the top of my head, I can't produce an example which would give different results. – Sergio Tulentsev Oct 31 '17 at 15:21
  • Right, refreshed my mind on nuances from the linked post. :) – Sergio Tulentsev Oct 31 '17 at 15:31
  • @meagar: I'm curious, _can_ diamond admins see deleted comments? :) – Sergio Tulentsev Oct 31 '17 at 15:38
  • @SergioTulentsev Yes, we can – user229044 Oct 31 '17 at 15:55

1 Answers1

0

Why did they decide to place it there, considering we know @looked_for is nil?

We don't know that. It might have value already. Remember, this is instance variable on a class object. And classes live for a long time.

The main vice of my question would be, what's the purpose of setting @looked_for[str_name] = 1

To short-circuit subsequent invocations of const_missing with the same parameter (since we tried it already and didn't find a matching constant). Again, this is possible because @looked_for persists between invocations of const_missing.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367