Ruby 2.5 has removed top level constant lookup

Amit Choudhary

Amit Choudhary

October 18, 2017

This blog is part of our  Ruby 2.5 series.

Ruby 2.4

irb> class Project
irb> end
=> nil

irb> class Category
irb> end
=> nil

irb> Project::Category
(irb):5: warning: toplevel constant Category referenced by Project::Category
 => Category

Ruby 2.4 returns the top level constant with a warning if it is unable to find a constant in the specified scope.

This does not work well in cases where we need constants to be defined with same name at top level and also in the same scope.

Ruby 2.5.0-preview1

irb> class Project
irb> end
=> nil

irb> class Category
irb> end
=> nil

irb> Project::Category
NameError: uninitialized constant Project::Category
Did you mean?  Category
	from (irb):5

Ruby 2.5 throws an error if it is unable to find a constant in the specified scope.

Here is the relevant commit and discussion.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.