Ruby 的 EOB 构造的用例
我最近在这个上下文中遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些称为“此处文档” ,多种语言都支持,并允许您创建多行字符串。实际上,您可以使用任何分隔符,而不仅仅是 EOB。 Ruby 对heredocs 有一些额外的功能:例如,
<<-EOB
中的-
允许您缩进分隔符。您可以像这样使用它:
Ruby 中的一些附加功能:
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:
Some additional features in Ruby: