Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

YY::XX always references XX if XX was defined #6931

Closed
ayamomiji opened this Issue · 6 comments

3 participants

@ayamomiji
# app/models/auth.rb
class Auth < ActiveRecord::Base; end
# app/models/auth/twitter.rb
class Auth::Twitter < Auth; end

# in console
>> Auth::Twitter
(rib-rails):1: warning: toplevel constant Twitter referenced by Auth::Twitter
=> Twitter # expect to be Auth::Twitter that defines in app/models/auth/twitter.rb

another example:

# app/models/user.rb
class User < ActiveRecord::Base; end

# in console
>> User::User::User::User::User::User::User::User::User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
(rib-rails):1: warning: toplevel constant User referenced by User::User
=> User(...)
@fxn
Owner
fxn commented

The second example is just the way Ruby works:

fxn@yeager:~ $ ruby -e 'class A; end; A::A'
-e:1: warning: toplevel constant A referenced by A::A

That happens because the rightmost A constant is found in one of the ancestors of the leftmost A, namely Object. Note that would not happen if A was a module, since Object would not be among its ancestors.

The first example is more interesting. Constant autoloading in Rails is based on const_missing. But for the same reason User::User works, the top-level Twitter constant is found via the qualified name Auth::Twitter if auth/twitter.rb has not yet been loaded. Therefore, const_missing is not even triggered.

@fxn fxn was assigned
@fxn
Owner
fxn commented

Hi @ayamomiji do you think we can close this issue?

@ayamomiji

Hi,
Thanks for reply.
I just added require_dependency 'auth/twitter' to every files that references Auth::Twitter to avoid this problem, and it works fine now.

@ayamomiji ayamomiji closed this
@fxn
Owner
fxn commented

Excellent, thanks!

@petRUShka

Is it a good idea to advice people to add require_dependency 'auth/twitter' in each file that use Auth::Twitter? It isn't convenient at least. And it isn't easy to find the reason of warning: toplevel constant Twitter referenced by like warnings (and hence errors off course) without extensive googling and stackoverflowing...

Is it possible for Rails to try to require auth/twitter' in case of such warning?

@fxn
Owner

The warning is issued by the interpreter, Rails does not see it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.