Reputation: 52347
Earlier today I had this problem, when I was running
sidekiq
I was getting the following error:
I, [2015-09-04T12:43:33.723243 #15197] INFO -- : Celluloid 0.17.1.2 is running in BACKPORTED mode. [ http://git.io/vJf3J ] Report is not a class /home/andreydeineko/employees/app/models/report.rb:1:in `<top (required)>'
but Report
was AR class/model without any typos.
It occurred the error was due to the fact, that in the project there was previously defined module called Report
.
Why is this problem with same class and module name occurred?
Is there any Ruby/Rails convention to not name the class and module with same name?
Upvotes: 0
Views: 4497
Reputation: 114138
Is there any Ruby/Rails convention to not name the class and module with same name?
The class
keyword does two things - it either create a new class and assigns it to the given constant or it reopens an existing class based on the given constant. (same for module
)
In the latter case, it will also raise an error if the constant is not the expected class. It does not matter whether it is a module or another object:
Report = 123
class Report
end
#=> TypeError: Report is not a class
Upvotes: 1
Reputation: 176352
You cannot reuse the same name for a Class
and a Module
. Internally, in Ruby modules are represented as class structures therefore they share the same object space.
Moreover, when you define a Module/Class, you can access the name as a constant.
class Report
def foo
p "report"
end
end
Report
=> Report
defined? Report
=> "constant"
in fact, you can also write
Report = Class.new do
def foo
p "report"
end
end
report = Report.new
report.foo
Long story short, if you define Report
as Class
, you cannot create a Module
with the same name.
Upvotes: 3