next up previous contents index
Search Next: mod_ruby Up: eRuby und mod_ruby Previous: Installation   Contents   Index

eRuby-Syntax und Anwendung

Es gibt verschiedene Arten, Ruby-Code einzubetten:

# <% Standardausgabe %>
$ cat foo
Hallo, <% print "Welt!" %>
$ eruby foo
Hallo, Welt!

# <%= Ergebnis des Ausdrucks %>
$ cat bar
Hallo, <%= "Welt!" %>
$ eruby bar
Hallo, Welt!

# % ist ein einzeiliger <% ... %>
$ cat quux
% x = 1 + 1
1 + 1 = <%= x %>
$ eruby quux
1 + 1 = 2

# <%# Kommentar (auch mehrzeilig) %>
$ cat baz
Hallo,<%# Das ist ein Kommentar %> Welt!
$ eruby baz
Hallo, Welt!

Man kann eRuby auch aus einem normalen Ruby-Programm heraus einsetzen.

require "eruby"
compiler = ERuby::Compiler.new
print compiler.compile_file(STDIN)

Das Resultat des eRuby-Compilers besteht im Wesentlichen aus print-Aufrufen, mit denen die übersetzte Datei auf die Standardausgabe geschrieben werden kann.

1 + 1 = <%= $resultat = 1 + 1 %>?
Stimmt, <%= $resultat %>.

# wird mit eRuby zu:
print "1 + 1 = "; print(( $resultat = 1 + 1)); print "?\n"
print "Stimmt, "; print(( $resultat )); print ".\n"



(C) 2002 by dpunkt.de, Armin Roehrl, Stefan Schmiedl, Clemens Wyss 2002-01-20