ArticleslgStudy

computer science

ERuby

ERuby is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand ERuby rather than just read about it. In short: Embedded Ruby (abbreviated as ERB or eRuby) is a template engine that embeds Ruby code into text documents. It is commonly used to generate dynamic content in HTML documents, which is a role similar to Active Server Pages, JavaServer Pages, PHP, and other server-side scripting languages. eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to…

Key takeaways

  • ERuby belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect ERuby to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of ERuby from memory before moving on to harder problems.

Reference excerpt

Embedded Ruby (abbreviated as ERB or eRuby) is a template engine that embeds Ruby code into text documents. It is commonly used to generate dynamic content in HTML documents, which is a role similar to Active Server Pages, JavaServer Pages, PHP, and other server-side scripting languages. eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to maintain. In Ruby on Rails, eRuby is commonly used in a view layer in order to generate dynamic web content. The view module of Ruby on Rails is responsible for displaying a response or output in a browser. In its simplest form, a view may consist of static HTML content; however, many Ruby on Rails applications will require dynamic content created by the controller (an action method) to be displayed in their view. This is achieved from Embedded Ruby templates, which allow Ruby code to be embedded within view documents. During rendering, the embedded code is evaluated and replaced with its resulting value at runtime. However, the ability to embed code in a view document risks bridging the clear separation present in the MVC frame. As a result, maintaining clear separation of responsibility among the model, view, and controller modules is left to the developer of the application.

Usage eRuby embeds Ruby code within a pair of <% and %> delimiters, which are then evaluated in-place. Apart from creating web pages, eRuby can also be used to create XML Documents, RSS feeds, and other forms of structured text files. Moreover, eRuby dynamically generates static files based on templates. These functionalities of eRuby can be found in the ERB Library. There are different types of tag markers used in ERB templates:

Expression tags Execution tags Comment tags

Expression tags The delimiter <%= %> indicates that the tag encloses an expression. Such tag starts with an opening tag delimiter followed by an equal to symbol, and ends with an end tag delimiter. During the rendering of the template, this piece of code gets substituted with the result of the code. If the evaluated result is not a string, it gets converted to a string before it is rendered.

Execution tags Execution tags are defined by <% %> and code enclosed in such tags is called a scriptlet. The code in the respective tag gets executed, and its result gets replaced in place of the scriptlet. Such tags require a matching <% end %> tag to denote the end of a functional block. The following ERB template generates an HTML list item four times:

The following is the resulting output:

As shown from the output, the text list item is printed four times. The scriptlet produces no text on its own; it only makes the enclosed statement to run multiple times.

Comments tags Comments are defined with the tag <%# %> and do not get rendered in the output. Such tags start with an open tag delimiter followed by a hash symbol and end with an end tag delimiter. The following is an example of a comment tag:

<%# ruby code %>

Ruby uses the same comment tags as eRuby, and all code after # is ignored, thereby generating nothing.

Other tags There are other tags common in both eRuby and Ruby, such as string substitution with #{string_name}, which is similar in languages such as Perl or PHP. Newlines in eRuby can be suppressed by adding a hyphen at the beginning of a end tag delimiter. The following is an example of the value of @name getting printed twice in the same line:

Implementations There are several implementations of eRuby:

ERB erubis ember

erb erb is an implementation of eRuby written purely in the Ruby programming language and included in the Ruby standard library. A template can be generated by running a piece of code written using the ERB object. A simple example is as follows:

This approach differs from string interpolation, which produces the output immediately when the string is constructed:However, constructing the string before the first variable is defined prevents the code from executing:Thus, the primary reason for using an ERB object is to write templates ahead of time, by binding variables and methods that may not exist at the given time. The template gets processed only when result is called on the ERB object. In order to get access to instance methods and instance variable of an object, ERB makes use of a binding object. Access to variables and methods of an object is given by the private binding object which exists in each Ruby class. It is easy to get access to methods and variables within the method of a class; however, to access variables of a different class, the class will have to expose its binding object via a public method. An example is as follows:

As shown, the binding object of the class ERBExample is exposed. Furthermore, the binding object is used to access the variables and methods of the class within one of its methods.

new() method of ERB The new method of the ERB object takes two more parameters. The first parameter specifies the template, and the second parameter specifies a safety level which makes the template run in a different thread. At the maximum safety level of four, ERB cannot use a binding object unless it is marked as trusted. The third parameter specifies optional modifiers, which can be used to control the adding of newlines to the output. For example, to ensure that ERB does not output newlines after tag ends, an ERB object can be created as shown below:

To only provide the third parameter and ignore the second parameter, use 0 as the input for second parameter. ERB has many other methods exposed which can be used to render a template. A full list of APIs exposed by the ERB object is listed in the ERB documentation.

Running ERB from Command-line The erb command-line provides a way to process ERB templates from the terminal.It reads a template file, evaluates embedded Ruby code, and prints the result in the output. Output redirection can be used to write the result to a file rather than printing it in standard output.

In the above example, output gets redirected to my_view.html.erb file. Additional third-party libraries can be loaded using the -r option followed by the name of the library, which is similar to the keyword require. The following example uses the IPAddr library:

For specifying safety levels, a number can be passed as a command-line argument using the -S option:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with ERuby

Start with the simplest possible case. Write down what ERuby claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to ERuby before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about ERuby ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of ERuby

In research
ERuby appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses ERuby in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
ERuby is common in secondary-school and first-year university syllabi. It links to neighbouring topics Free computer libraries, Ruby (programming language), Template engines, so understanding it makes those chapters shorter.
In everyday life
Look for ERuby outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “ERuby” →

How to study ERuby in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what ERuby means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain ERuby out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is ERuby in simple terms?

Embedded Ruby (abbreviated as ERB or eRuby) is a template engine that embeds Ruby code into text documents. It is commonly used to generate dynamic content in HTML documents, which is a role similar to Active Server Pages, JavaServer Pages, PHP, and other server-side scripting languages. eRuby comb…

Why does ERuby matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study ERuby?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on ERuby.

Tags

  • Free computer libraries
  • Ruby (programming language)
  • Template engines

Keep exploring