Ruby 的 EOB 构造的用例

发布于 2024-11-17 06:54:01 字数 1581 浏览 4 评论 0原文

我最近在这个上下文中遇到了 Ruby EOB / -EOB 构造(来自 Ruby id3 库) :

def initialize(...)
  # ...

  instance_eval <<-EOB
    class << self

      def parse
        # ...
        # Method code
        # ...
      end   
  EOB 
  self.parse # now we're using the just defined parsing routine

  # ...
end

我知道代码用于动态生成方法,但我想知道它是否会可以使用方法中的 EOB 片段。我想编写一个方法来生成一些其他方法代码,这些代码将包含在另一个类中。这听起来有点令人困惑,我将尝试用一些简化的代码示例来说明我的意图:

# This class reads the code of another 
# Ruby class and injects some methods
class ReadAndInject

  # The method which defines another method
  def get_code_to_be_injected
    "\tdef self.foo\n"+
    "\t\tputs 'bar'\n"+
    "\tend\n"
  end

  # Main entry point, reads a generated Ruby Class
  # and injects specific methods within it
  def read_and_inject

    # Assume placeholder for currently read line,
    # add the generated code within
    current_line += "\n#{get_code_to_be_injected}"
  end

end # class ReadAndInject

这会起作用,因为要注入的方法已正确添加。然而我想知道使用 EOB 构造是否会产生一些优势(例如,更好的代码可见性,因为不需要添加繁琐的选项卡或字符串连接。

总而言之,这是一个很好的用例对于EOB? 它看起来像是一个阴暗但强大的构造,我已经回避它,用谷歌搜索和 stackoverflow 搜索它,但没有其他重要的代码示例比 来自 RubyCocoa 的一个被返回。我最近才开始在 Ruby 中使用元结构,所以请温柔一点:-)

提前致谢!

I recently came across the Ruby EOB / -EOB construct within this context (from the Ruby id3 library) :

def initialize(...)
  # ...

  instance_eval <<-EOB
    class << self

      def parse
        # ...
        # Method code
        # ...
      end   
  EOB 
  self.parse # now we're using the just defined parsing routine

  # ...
end

I understand that the code is used to generate a method on the fly, yet I would like to know if it would be possible to use the EOB snippet within a method. I would like to write a method which generates some other method code , which is to be included in yet another class. This sounds a bit confusing, I'll try to illustrate my intention with some simplified code samples :

# This class reads the code of another 
# Ruby class and injects some methods
class ReadAndInject

  # The method which defines another method
  def get_code_to_be_injected
    "\tdef self.foo\n"+
    "\t\tputs 'bar'\n"+
    "\tend\n"
  end

  # Main entry point, reads a generated Ruby Class
  # and injects specific methods within it
  def read_and_inject

    # Assume placeholder for currently read line,
    # add the generated code within
    current_line += "\n#{get_code_to_be_injected}"
  end

end # class ReadAndInject

This would work, since the method to be injected is added correctly. Yet I was wondering if using the EOB construct would yield some advantages (e.g. better visibility of the code, since no cumbersome tabs or string concatenations would have to be added.

To conclude, is this a good use case for EOB ?
It seems like a shady yet powerful construct, I've ducked it, googled and stackoverflow'd it yet no significant code samples other than one from RubyCocoa were returned. I've only recently started to use meta constructs in Ruby, so please be gentle :-)

Thanks in advance!

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

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

发布评论

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

评论(1

辞慾 2024-11-24 06:54:01

这些称为“此处文档” ,多种语言都支持,并允许您创建多行字符串。实际上,您可以使用任何分隔符,而不仅仅是 EOB。 Ruby 对heredocs 有一些额外的功能:例如,<<-EOB 中的- 允许您缩进分隔符。

您可以像这样使用它:

def code_to_be_injected
  <<-EOS
    def self.foo
      puts 'bar'
    end
  EOS
end

Ruby 中的一些附加功能:

myvar = 42
<<EOS
variable: #{myvar}
EOS #=> "variable: 42"

<<'EOS'
variable: #{myvar}
EOS #=> "variable: #{myvar}"

print <<A, <<B
This will appear first
A
and this second
B

These are called "here documents", which are supported by several languages, and allow you to make a multi-line string. You can actually use any delimiter, not just EOB. Ruby has some extra features for heredocs: for example, the - in <<-EOB allows you to indent the delimiter.

You might use it like this:

def code_to_be_injected
  <<-EOS
    def self.foo
      puts 'bar'
    end
  EOS
end

Some additional features in Ruby:

myvar = 42
<<EOS
variable: #{myvar}
EOS #=> "variable: 42"

<<'EOS'
variable: #{myvar}
EOS #=> "variable: #{myvar}"

print <<A, <<B
This will appear first
A
and this second
B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文