Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid for Ruby class to extend itself but under a module?

Tags:

ruby

Is it valid syntax to have a Ruby class extend itself, but under a separate module? For instance, I have an ErrorsController class..

class ErrorsController < ApplicationController
 ....
end

But I want to have a different behavior if under a module..

class Share::ErrorsController < ErrorsController
 ....
end

Here ErrorsController is extending ErrorsController, which works fine. But then I wrote it in long form:

module Share
  class ErrorsController < ErrorsController
  end
end

There seemed to be problems sometime..

like image 594
GN. Avatar asked Jan 27 '23 03:01

GN.


1 Answers

To unambiguously refer to the "top-level" ErrorsController, the typical technique is to add two colons before it. So for example:

module Share
  class ErrorsController < ::ErrorsController
  end
end
like image 53
Max Avatar answered Feb 15 '23 10:02

Max