如何在不使用 eval 的情况下动态调用类?

发布于 2024-07-13 12:31:26 字数 713 浏览 9 评论 0原文

是否可以去掉下面的eval语句? 下面的代码过滤掉从 BaseClass 类型派生的所有类。 之后这些类被实例化并调用方法“hello”。

module MySpace

  class BaseClass
    def hello; print "\nhello world"; end
  end

  class A<BaseClass
    def hello; super; print ", class A was here"; end
  end

  class B<BaseClass
    def hello; super; print ", I'm just a noisy class"; end
  end

  MySpace.constants.each do | e |
    c=eval(e)
    if c < BaseClass
      c.new.hello
    end
  end

end

所以执行后输出是:

hello world, I'm just a busy class
你好,世界,A 类在这里

我认为不必要的使用 eval 是邪恶的。 我不确定这里是否强制使用 eval 。 是否有一种更智能的方法来动态调用“BaseClass”类型中的所有类?

Is it possible to get rid of the eval statement below? The code below filters out all classes which are derived from type BaseClass. Afterwards those classes are instantiated and method 'hello' is called.

module MySpace

  class BaseClass
    def hello; print "\nhello world"; end
  end

  class A<BaseClass
    def hello; super; print ", class A was here"; end
  end

  class B<BaseClass
    def hello; super; print ", I'm just a noisy class"; end
  end

  MySpace.constants.each do | e |
    c=eval(e)
    if c < BaseClass
      c.new.hello
    end
  end

end

So after execution the output is:

hello world, I'm just a noisy class
hello world, class A was here

I think unnecessary use of eval is evil. And I'm not sure if the use of eval is mandatory here. Is there is a smarter way in invoking all classes from type "BaseClass" dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-07-20 12:31:26
c = MySpace.const_get(e)
c = MySpace.const_get(e)
氛圍 2024-07-20 12:31:26

eval 是我所知道的将字符串转换为常量的唯一方法。 甚至 Rails 也是这样做的:
http://api.rubyonrails.com/classes/Inflector.html#M001638

奇怪的是常量返回字符串。

eval is the only way I know of to turn a string into a constant. Its even the way rails does it:
http://api.rubyonrails.com/classes/Inflector.html#M001638

The odd thing is that constants returns strings.

小瓶盖 2024-07-20 12:31:26

您是否看过class_eval

------------------------------------------------------ Module#class_eval
     mod.class_eval(string [, filename [, lineno]])  => obj
     mod.module_eval {|| block }                     => obj
------------------------------------------------------------------------
     Evaluates the string or block in the context of _mod_. This can be
     used to add methods to a class. +module_eval+ returns the result of
     evaluating its argument. The optional _filename_ and _lineno_
     parameters set the text for error messages.

        class Thing
        end
        a = %q{def hello() "Hello there!" end}
        Thing.module_eval(a)
        puts Thing.new.hello()
        Thing.module_eval("invalid code", "dummy", 123)

    produces:

        Hello there!
        dummy:123:in `module_eval': undefined local variable
            or method `code' for Thing:Class

Have you looked at class_eval instead?

------------------------------------------------------ Module#class_eval
     mod.class_eval(string [, filename [, lineno]])  => obj
     mod.module_eval {|| block }                     => obj
------------------------------------------------------------------------
     Evaluates the string or block in the context of _mod_. This can be
     used to add methods to a class. +module_eval+ returns the result of
     evaluating its argument. The optional _filename_ and _lineno_
     parameters set the text for error messages.

        class Thing
        end
        a = %q{def hello() "Hello there!" end}
        Thing.module_eval(a)
        puts Thing.new.hello()
        Thing.module_eval("invalid code", "dummy", 123)

    produces:

        Hello there!
        dummy:123:in `module_eval': undefined local variable
            or method `code' for Thing:Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文