Skip to content

Commit 684ac23

Browse files
committed
[DOC] New doc about erb executable
1 parent 1512314 commit 684ac23

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

_doc/erb_executable.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# \ERB Executable
2+
3+
The `erb` executable gives command-line access to ERB template processing.
4+
5+
The executable is installed with \ERB, which is part of the Ruby installation.
6+
7+
For a quick summary, type:
8+
9+
```
10+
$ erb --help
11+
```
12+
13+
The format of the command is
14+
`erb [_options_] [_filepaths_]`,
15+
where:
16+
17+
- `_options_` are zero or more [ERB options][options].
18+
- `_filepaths_` are zero or more paths to files containing [ERB input][input].
19+
20+
## Input
21+
22+
With no *_filepaths_* given, \ERB reads input from the standard input:
23+
24+
```bash
25+
$ echo "<%= RUBY_VERSION %>" | erb # Prints the ERB version string.
26+
```
27+
28+
With *_filepaths_* given, \ERB reads input from each of the given files:
29+
30+
```bash
31+
$ cat t.erb
32+
<%= RUBY_VERSION %>
33+
<%= Time.now %>
34+
$ cat u.erb
35+
% Encoding.list.take(4).each do |encoding|
36+
* <%= encoding %>
37+
% end
38+
$ erb t.erb u.erb
39+
3.4.5
40+
2025-09-24 00:23:00 +0100
41+
* ASCII-8BIT
42+
* UTF-8
43+
* US-ASCII
44+
* UTF-16BE
45+
```
46+
47+
48+
## Options
49+
50+
### `-d`, `--debug`: Set $DEBUG
51+
52+
Use option `-d` or `--debug` to turn on debugging output:
53+
54+
```bash
55+
$ echo "<%= $DEBUG %>" | erb
56+
"false"
57+
$echo "<%= $DEBUG %>" | erb --debug
58+
"true"
59+
```
60+
61+
### `-E`, `--encoding`: Set Encodings
62+
63+
Use option `-E` or `--encoding` to set the default external encoding to `_ex_`
64+
and, if `_in_` is given, to set the default internal encoding to `_in_`.
65+
66+
Each encoding, `ex` and `in`, must be the name of an Encoding:
67+
68+
```
69+
erb -E ASCII-8BIT:ASCII-8BIT t.erb
70+
```
71+
72+
### `-h`, `--help`: Print Help
73+
74+
Use option `-h` or `--help` to print \ERB help text:
75+
76+
```bash
77+
$ erb --help
78+
```
79+
80+
### `-n`: Print Source with Line Numbers
81+
82+
Use option `-n` with option `-x` to print the output of ERB#src,
83+
with numbered lines:
84+
85+
```bash
86+
$ cat t.erb
87+
<%= RUBY_VERSION %>
88+
<%= Time.now %>
89+
$ erb -n -x t.erb
90+
1 #coding:UTF-8
91+
2 _erbout = +''; _erbout.<<(( RUBY_VERSION ).to_s); _erbout.<< "\n".freeze
92+
3 ; _erbout.<<(( Time.now ).to_s); _erbout.<< "\n".freeze
93+
4 ; _erbout
94+
```
95+
96+
Using option `-n` without option `-x` has no effect:
97+
98+
```bash
99+
$ erb -n t.erb
100+
3.4.5
101+
2025-09-23 02:44:57 +0100
102+
```
103+
104+
### `-P`: Disable Execution Tag Shorthand
105+
106+
By default, `erb` enables [execution tag shorthand][execution tag shorthand]:
107+
108+
```
109+
$ cat u.erb
110+
% Encoding.list.take(4).each do |encoding|
111+
* <%= encoding %>
112+
% end
113+
$ erb u.erb
114+
* ASCII-8BIT
115+
* UTF-8
116+
* US-ASCII
117+
* UTF-16BE
118+
```
119+
120+
You can use option `-P` to disable the shorthand:
121+
122+
```
123+
$ erb -P u.erb # Raises NameError: "undefined local variable or method 'encoding'"
124+
```
125+
126+
### `-r`: Load Library
127+
128+
You can use option `-r` to load a library;
129+
the option may be given multiple times, to load multiple libraries:
130+
131+
```
132+
$ erb -r csv -r bigdecimal t.erb
133+
```
134+
135+
### `-T`: Set Trim Mode
136+
137+
You can use option `-T` to set the trim mode.
138+
139+
The values for the option are:
140+
141+
- `'0'`, meaning `'%'`; enable execution tag shorthand;
142+
see [execution tag shorthand][execution tag shorthand].
143+
- `'1'`, meaning `'%>'`: enable execution tag shorthand and omit newline for each line ending with `'%>'`;
144+
see [suppressing unwanted newlines][suppressing unwanted newlines].
145+
- `'2'`, meaning `'<>'`: to suppress the trailing newline for each line
146+
that both begins with `'<%'` and ends with `'%>'`;
147+
see [suppressing unwanted newlines][suppressing unwanted newlines].
148+
- `'-'`, meaning `'%-'`: enable execution tag shorthand and omit each blank line ending with `'-%>'`.
149+
see [execution tag shorthand][execution tag shorthand]
150+
and [suppressing unwanted blank lines][suppressing unwanted blank lines].
151+
152+
Example:
153+
154+
```bash
155+
$ erb -T 0 t.erb
156+
```
157+
158+
### `-U`: Set Default Encodings to UTF-8
159+
160+
You can use option `-U` to set both external and internal encodings to UTF-8:
161+
162+
```bash
163+
$ erb -U t.erb
164+
```
165+
166+
### `-v`: Set $VERBOSE
167+
168+
Use option `-V` to turn on verbose output:
169+
170+
```bash
171+
$ $ "<%= $VERBOSE %>" | erb
172+
"false"
173+
$ echo "<%= $VERBOSE %>" | erb -v
174+
"true"
175+
```
176+
177+
### --help`: Print \ERB Version
178+
179+
Use option `-h` or `--help` to print the \ERB version string:
180+
181+
```bash
182+
$ erb --version
183+
```
184+
185+
### `-x`: Print Source
186+
187+
Use option `-x` to print the output of ERB#src:
188+
189+
```bash
190+
$ cat t.erb
191+
<%= RUBY_VERSION %>
192+
<%= Time.now %>
193+
$ erb -x t.erb
194+
#coding:UTF-8
195+
_erbout = +''; _erbout.<<(( RUBY_VERSION ).to_s); _erbout.<< "\n".freeze
196+
; _erbout.<<(( Time.now ).to_s); _erbout.<< "\n".freeze
197+
; _erbout
198+
```
199+
200+
### `name=value`: Set the Value of a Variable
201+
202+
You can use option `name=value` to set the value of the variable named `name`
203+
to the given `value`.
204+
205+
The option may be given multiple times to set multiple variables:
206+
207+
```bash
208+
$ echo "<%= foo %> <%= bar %>" | erb foo=1 bar=2
209+
"1 2"
210+
```
211+
212+
[erb.new]: https://docs.ruby-lang.org/en/master/ERB.html#method-c-new.
213+
[execution tag shorthand]: rdoc-ref:ERB@Shorthand+Format+for+Execution+Tags
214+
[input]: rdoc-ref:erb_executable.md@Input
215+
[options]: rdoc-ref:erb_executable.md@Options
216+
[suppressing unwanted blank lines]: rdoc-ref:ERB@Suppressing+Unwanted+Blank+Lines
217+
[suppressing unwanted newlines]: rdoc-ref:ERB@Suppressing+Unwanted+Newlines

0 commit comments

Comments
 (0)