(In Ruby) allowing mixed-in class methods access to class constants

后端 未结 3 1471
星月不相逢
星月不相逢 2021-02-20 00:32

I have a class with a constant defined for it. I then have a class method defined that accesses that class constant. This works fine. An example:

#! /usr/bin/         


        
3条回答
  •  暖寄归人
    2021-02-20 00:47

    The problem is, if you just write Const it is evaluated on Module creation time. You have to use Module#const_get instead like this: const_get(:Const). This is evaluated at runtime when the method is executed. So this happens in your class and not in your module.

提交回复
热议问题