Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Ruby class without namespace

There are two classes in Rails application: one with namespace, other - without. Both have the same name.

How to call a class without namespace from within a namespace?

class MyClass < ActiveRecord::Base
end

module MyNamespace
    class MyClass < Base
    end

    class OtherClass < Base
       def some_method
           #Intend to create MyClass model, but MyNamespace::MyClass is created
           mobj = MyClass
       end
    end
end
like image 370
Paul Avatar asked Jul 24 '26 04:07

Paul


1 Answers

To break out of the current namespace, just write:

mobj = ::MyClass
like image 187
spickermann Avatar answered Jul 25 '26 17:07

spickermann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!