What to use, eruby or erb?

What is the difference between eruby and erb? What considerations prompted me to choose this or that?

My application creates configuration files for network devices (routers, load balancers, firewalls, etc.). My plan is to create template configuration files using the built-in ruby โ€‹โ€‹(via eruby or erb) in the source files to do things like iteratively generate all the interface configuration blocks for the router (all of these blocks are very similar, they differ only label and IP address). For example, I may have a configuration template file, for example:

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 launched through the built-in Ruby interpreter (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 
+4
source share
3 answers

It doesnโ€™t matter, they are both the same. erb is pure ruby, eruby is written in C, so it is a little faster.

erubis (third) - pure ruby โ€‹โ€‹and faster than those listed above. But I doubt the speed of this is a bottleneck for you, so just use erb. This is part of the Ruby standard library.

+9
source

Eruby is an external executable, and erb is a library in Ruby. You would use the former if you needed independent processing of your template files (for example, a quick and dirty PHP replacement), and the second if you needed to process them in the context of some other Ruby script. Most often, using ERB is simply because it is more flexible, but I admit that it was my fault that I went into eruby to execute .rhtml files for fast, small websites.

+2
source

I am doing something similar using erb and performance is right for me.

As Jordi said, it depends on the context in which you want to run this: if you are literally going to use templates like the one you specified, eruby will probably work better, but I think you're really going to pass variables into the template, in which case you want erb.

Just for reference, when using erb, you need to pass it a binding for the object from which you want to take variables, something like this:

 device = Device.new device.add_interface("GigabitEthernet1/1", "10.5.16.1") device.add_interface("GigabitEthernet1/2", "10.5.17.1") template = File.read("/path/to/your/template.erb") config = ERB.new(template).result(device.binding) 
0
source

Source: https://habr.com/ru/post/1276760/


All Articles