eruby
acts as a filter, plain and simple. Any text within the input
file is passed through untouched, with the following exceptions:
Expression
|
Description
|
<% ruby code %>
|
The Ruby code between the delimiters
is replaced with its output. |
<%= ruby expression %>
|
The Ruby expression
between the delimiters is replaced with its value. |
<%# ruby code %>
|
The Ruby code between the
delimiters is ignored (useful for testing). |
|
You invoke
eruby
as:
eruby [
options
] [
document
]
|
If the
document is omitted,
eruby
will read from standard
input. The command-line options for
eruby
are shown in Table
14.1 on page 149.
Command-line options for eruby
Option
|
Description
|
-d , --debug
|
Sets $DEBUG to true . |
-K
kcode
|
Specifies an alternate coding system
(see page 137). |
-M
mode
|
Specifies runtime mode, one of:
f
|
filter mode |
c
|
CGI mode (prints errors as HTML, sets $SAFE=1) |
n
|
NPH-CGI mode (prints extra HTTP headers, sets $SAFE=1) |
|
|
-n , --noheader
|
Disables CGI header output. |
-v , --verbose
|
Enables verbose mode. |
--version
|
Prints version information and exits. |
|
|
Let's look at some simple examples. We'll run the
eruby
executable
on the following input.
This text is <% a = 100; puts "#{a}% Live!" %>
|
eruby
substitutes the expression between the delimiters and
produces
Using the <%= form acts as if you printed the value of the
expression. For instance, the input
<%a = 100%>This text is almost <%=a%> degrees! Cool!
|
replaces the
=a
with the value of
a
.
This text is almost 100 degrees! Cool!
|
And, of course, you can embed Ruby within a more complex document type,
such as HTML.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>eruby example</title>
</head>
<body>
<h1>Enumeration</h1>
<ul>
<%(1..10).each do|i|%>
<li>number <%=i%></li>
<%end%>
</ul>
<h1>Environment variables</h1>
<table>
<%ENV.keys.sort.each do |key|%>
<tr>
<th><%=key%></th><td><%=ENV[key]%></td>
</tr>
<%end%>
</table>
</body>
</html>
|