ERuby
Encyclopedia
eRuby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML
document, similar to ASP
, JSP
and PHP
.
Here are a few examples of eRuby usage:
One line of Ruby
Output like hello from
Alternatively, lines starting with a
Multiple lines
These can appear less graceful because the beginning and ending are not quite the same. They function like blocks in Ruby and are terminated by
Outputting:
The same code could also be written as:
Expression result substitution
Comments
Other things common in eRuby are simply common in Ruby, such as string substitution with
programming language.
. According to its home page, it runs faster than eruby and erb and has several useful options, including alternate tags allowing for valid XML
.
HTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
document, similar to ASP
Active Server Pages
Active Server Pages , also known as Classic ASP or ASP Classic, was Microsoft's first server-side script engine for dynamically-generated Web pages. Initially released as an add-on to Internet Information Services via the Windows NT 4.0 Option Pack Active Server Pages (ASP), also known as Classic...
, JSP
JavaServer Pages
JavaServer Pages is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types...
and PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
.
Usage
eRuby allows Ruby code to be embedded within a pair of<%
and %>
delimiters. These embedded code blocks are then evaluated in-place (they are replaced by the result of their evaluation).Here are a few examples of eRuby usage:
One line of Ruby
<% ruby code %>
Output like hello from
<% print "hello" %>
would replace the ruby code between the delimiters.Alternatively, lines starting with a
%
sign are interpreted as Ruby as well:% ruby code
Multiple lines
These can appear less graceful because the beginning and ending are not quite the same. They function like blocks in Ruby and are terminated by
<% end %>
. They are commonly used as looping constructs, which appear like this:- list item
<% 3.times do %>
<% end %>
Outputting:
- list item
- list item
- list item
The same code could also be written as:
- list item
% 3.times do
% end
Expression result substitution
<%= ruby expression %>- Value evaluated from expression like 11 from
7 + 4
would replace the ruby expression between the delimiters. Often these are only one line.Comments
<%# ruby code %>- this is the same as a comment in Ruby. All Ruby code after the # is ignored and generates nothing.
Other things common in eRuby are simply common in Ruby, such as string substitution with
#{string_name}, which is similar in languages such as Perl or PHP.
eruby
eruby is an implementation of eRuby written in the CC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
programming language.
erb
erb is an implementation of eRuby written purely in the Ruby programming language and included in the Ruby standard library.erubis
erubis is an implementation of eRuby implemented in Ruby and also in JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
. According to its home page, it runs faster than eruby and erb and has several useful options, including alternate tags allowing for valid XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
.
ember
ember is a pure Ruby implementation of eRuby. It allows debugging of eRuby templates, improves their composability, and provides powerful shorthand eRuby directives.See also
- mod rubyMod rubymod_ruby is a module that embeds the Ruby interpreter into the Apache web server to allow Ruby code to execute natively, faster than other CGI methods. Its drawback is that the characteristic sharing of classes among Apache processes is not safe for multiple applications mod_ruby is a module that...
- Phusion PassengerPhusion PassengerPhusion Passenger is a module for the Apache HTTP Server and nginx for deployment of Ruby applications, including those built using the Ruby on Rails framework. It is available as a Gem package and is supported on Unix-like operating systems...
(mod_rails) - HamlHamlHaml is a lightweight markup language that is used to describe the XHTML of any web document without the use of traditional inline coding. It’s designed to address many of the flaws in traditional templating engines, as well as making markup as elegant as it can be...
- RDocRDocRDoc, designed by Dave Thomas, is an embedded documentation generator for the Ruby programming language.It analyzes Ruby source code, generating a structured collection of pages for Ruby objects and methods.Code comments can be added in a natural style....
- MarkabyMarkabyMarkaby is a small Ruby library for writing HTML code in pure Ruby. It is an alternative to templating languages such as ERb and HAML which combine Ruby code with some form of markup...
External links
- "Ruby and the web", a chapter from "The Pragmatic Programmer's Guide"