Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Ruby Programming
Previous Page Home Next Page

Using eruby

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

This text is 100% Live!

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>
Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire