Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which to use, eruby or erb?

Tags:

ruby

erb

eruby

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
like image 614
Brent Chapman Avatar asked Sep 16 '08 17:09

Brent Chapman


People also ask

Is ERB Ruby?

ERB (or Ruby code generated by ERB ) returns a string in the same character encoding as the input string. When the input string has a magic comment, however, it returns a string in the encoding specified by the magic comment.

What does ERB stand for Ruby?

ERB stands for Embedded Ruby.

What is an ERB file?

What is a ERB file. ERB files contain source code written in a programming language of the same name. The ERB language is essentially a Ruby templating language. ERB files are saved in a plain text format which allows them to be opened in any text editing program.

What are the necessary steps to get from file ERB to file HTML?

erb file into a string, create an ERB object, and then output the result into an html file, passing the current binding to the ERB. result method. where @my_erb_string . You could then write the html string into an html file.


2 Answers

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.

like image 178
Jordi Bunster Avatar answered Oct 12 '22 21:10

Jordi Bunster


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.

like image 20
Daniel Spiewak Avatar answered Oct 12 '22 21:10

Daniel Spiewak