I want to extend the class using class_eval, and trying to access the constant from the source class, I got an error:
NameError: uninitialized constant HIS_CONSTANT from./my_module.rb:35:in `show_his_constant 'from (irb): 4
I tested a sample program and cannot make it work. Can someone check and see why this is not working? Thanks!
module MyModule puts "start my module" def mytest puts "mytest" end module YourModule def yourtest puts "yourtest" end end end module MyModule module YourModule module HisModule HIS_CONSTANT = 'THIS_IS_A_CONSTANT' end end end module MyModule module YourModule class HisClass include HisModule def show_constant puts HIS_CONSTANT end end end end MyModule::YourModule::HisClass.class_eval do def show_his_constant puts HIS_CONSTANT end end
By the way, I know that this method may work:
MyModule::YourModule::HisClass.class_eval do def show_his_constant puts MyModule::YourModule::HisModule::HIS_CONSTANT end end
But I do not want to use the namespace for access, as it should already be included.
Jimmy source share