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"