Ruby: How to include classes with names in the global namespace in IRB

For some reason, I want to use classes / modules with a namespace, since they are in the global namespace in the IRB.

For example, I have a module MyCoreand a class MyUserinside it. Is there any mechanism or hook for IRB to enable MyCore::MyUserin such a way that I can only name it MyUser.newwithout prefixing it with MyCore?

+4
source share
4 answers

You can just do

include MyCore

myUser = MyUser.new

Using it includeadds all the constants in the module to your current class.

class WhereIWantToIncludeMyCore
  include MyCore

  def initialize
    user = MyUser.new
  end
end

, class, Object.

+4

-

MU = MyCore::MyUser

, .

0

You can always do

MyUser = MyCore::MyUser

If you want to get all included modules / classes:

MyCore.constants.each do |const|
  Object.const_set(const, MyCore.const_get(const))
end
0
source

i research this problem and i found this talking and scripts

but I thought this gem is wrap_in_moduledoing best practice

wrap_in_module

hope this helps.

0
source

Source: https://habr.com/ru/post/1539586/


All Articles