使用哪个,eruby 还是 erb?
eruby 和 erb 有什么区别? 哪些考虑因素会促使我选择其中之一?
我的应用程序正在为网络设备(路由器、负载平衡器、防火墙等)生成配置文件。 我的计划是模板化配置文件,在源文件中使用嵌入式 ruby(通过 eruby 或 erb)来执行诸如迭代生成路由器的所有接口配置块之类的操作(这些块都非常相似,仅在标签上有所不同)和 IP 地址)。 例如,我可能有一个像这样的配置模板文件:
hostname sample-router
<%=
r = String.new;
[
["GigabitEthernet1/1", "10.5.16.1"],
["GigabitEthernet1/2", "10.5.17.1"],
["GigabitEthernet1/3", "10.5.18.1"]
].each { |tuple|
r << "interface #{tuple[0]}\n"
r << " ip address #{tuple[1]} netmask 255.255.255.0\n"
}
r.chomp
%>
logging 10.5.16.26
当通过嵌入式 ruby 解释器(erb 或 eruby)运行时,会产生以下输出:
hostname sample-router
interface GigabitEthernet1/1
ip address 10.5.16.1 netmask 255.255.255.0
interface GigabitEthernet1/2
ip address 10.5.17.1 netmask 255.255.255.0
interface GigabitEthernet1/3
ip address 10.5.18.1 netmask 255.255.255.0
logging 10.5.16.26
What's the difference between eruby and erb? What considerations would drive me to choose one or the other?
My application is generating config files for network devices (routers, load balancers, firewalls, etc.). My plan is to template the config files, using embedded ruby (via either eruby or erb) within the source files to do things like iteratively generate all the interface config blocks for a router (these blocks are all very similar, differing only in a label and an IP address). For example, I might have a config template file like this:
hostname sample-router
<%=
r = String.new;
[
["GigabitEthernet1/1", "10.5.16.1"],
["GigabitEthernet1/2", "10.5.17.1"],
["GigabitEthernet1/3", "10.5.18.1"]
].each { |tuple|
r << "interface #{tuple[0]}\n"
r << " ip address #{tuple[1]} netmask 255.255.255.0\n"
}
r.chomp
%>
logging 10.5.16.26
which, when run through an embedded ruby interpreter (either erb or eruby), produces the following output:
hostname sample-router
interface GigabitEthernet1/1
ip address 10.5.16.1 netmask 255.255.255.0
interface GigabitEthernet1/2
ip address 10.5.17.1 netmask 255.255.255.0
interface GigabitEthernet1/3
ip address 10.5.18.1 netmask 255.255.255.0
logging 10.5.16.26
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Eruby 是一个外部可执行文件,而 erb 是 Ruby 中的一个库。 如果您想要独立处理模板文件(例如快速而肮脏的 PHP 替换),则可以使用前者,如果您需要在其他 Ruby 脚本的上下文中处理它们,则可以使用后者。 使用 ERB 更常见,因为它更灵活,但我承认,我曾因涉足 eruby 来为快速的小实用程序网站执行
.rhtml
文件而感到内疚。Eruby is an external executable, while erb is a library within Ruby. You would use the former if you wanted independent processing of your template files (e.g. quick-and-dirty PHP replacement), and the latter if you needed to process them within the context of some other Ruby script. It is more common to use ERB simply because it is more flexible, but I'll admit that I have been guilty of dabbling in eruby to execute
.rhtml
files for quick little utility websites.我正在使用 erb 做类似的事情,并且性能对我来说很好。
正如 Jordi 所说,这取决于您想要在什么上下文中运行它 - 如果您确实要使用您列出的模板,那么 eruby 可能会工作得更好,但我猜您实际上会通过模板的变量,在这种情况下你需要 erb.
仅供参考,在使用 erb 时,您需要将要从中获取变量的对象的绑定传递给它,如下所示:
I'm doing something similar using erb, and the performance is fine for me.
As Jordi said though, it depends what context you want to run this in - if you're literally going to use templates like the one you listed, eruby would probably work better, but I'd guess you're actually going to be passing variables to the template, in which case you want erb.
Just for reference, when using erb you'll need to pass it the binding for the object you want to take variables from, something like this:
没关系,它们都是一样的。 erb 是纯 ruby,eruby 是用 C 编写的,所以速度更快一些。
erubis(第三个)是纯红宝石,并且比上面列出的两个更快。 但我怀疑它的速度对你来说是瓶颈,所以就使用 erb 吧。 它是 Ruby 标准库的一部分。
Doesn't really matter, they're both the same. erb is pure ruby, eruby is written in C so it's a bit faster.
erubis (a third one) is pure ruby, and faster than both the ones listed above. But I doubt the speed of that is the bottleneck for you, so just use erb. It's part of Ruby Standard Library.