在ruby中使用class

编程入门 行业动态 更新时间:2024-10-09 17:31:25
ruby中使用class_eval时,如何访问原始类中的常量?(When using class_eval in ruby, how to access the constant in the original class?)

我想使用class_eval扩展一个类,并且在尝试从原始类访问常量时,我​​得到一个错误说:

NameError:未初始化的常量HIS_CONSTANT来自./my_module.rb:35:来自(irb)的“show_his_constant”:4

我测试了一个示例程序,但无法使其正常工作。 有人可以检查,看看为什么这不起作用? 谢谢!

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

顺便说一句,我知道这种方式可行:

MyModule::YourModule::HisClass.class_eval do def show_his_constant puts MyModule::YourModule::HisModule::HIS_CONSTANT end end

但我不想使用命名空间来访问,因为它应该已经包含在内。

I want to extend a class using class_eval, and while trying to access the constant from the original class, I got an error saying:

NameError: uninitialized constant HIS_CONSTANT from ./my_module.rb:35:in `show_his_constant' from (irb):4

I tested with a sample program, and can't make it work. Can someone pls 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

BTW, I know this way can work:

MyModule::YourModule::HisClass.class_eval do def show_his_constant puts MyModule::YourModule::HisModule::HIS_CONSTANT end end

But I don't want to use namespace to access, since it should already be included.

最满意答案

你必须使用ruby 1.8,因为你的代码看起来像1.9中所写的那样工作。

在1.8中,问题似乎是常量被定义在块的定义的上下文中(当你开始编写MyModule::YourModule::HisClass.class_eval时,无论是什么样的self )。 您可以通过使用Module.const_get延迟常量绑定,直到self成为MyModule::YourModule::HisClass的Module.const_get 。

MyModule::YourModule::HisClass.class_eval do def show_his_constant puts self.class.const_get(:HIS_CONSTANT) end end irb 1.8.7> MyModule::YourModule::HisClass.new.show_his_constant THIS_IS_A_CONSTANT

You must be using ruby 1.8 as your code appears to work as written in 1.9.

In 1.8 the problem seems to be that the constant is bound in the context of where the block is defined (whatever self was when you started to write MyModule::YourModule::HisClass.class_eval). You can delay the constant binding until self has become an instance of MyModule::YourModule::HisClass by using Module.const_get.

MyModule::YourModule::HisClass.class_eval do def show_his_constant puts self.class.const_get(:HIS_CONSTANT) end end irb 1.8.7> MyModule::YourModule::HisClass.new.show_his_constant THIS_IS_A_CONSTANT

更多推荐

本文发布于:2023-08-04 02:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ruby   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!