File: irb.html

package info (click to toggle)
rubybook 0.2.1-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 4,248 kB
  • ctags: 1,042
  • sloc: xml: 60,486; makefile: 25
file content (642 lines) | stat: -rw-r--r-- 24,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
       BODY    { margin-left: 1in;
                 width: 6in;
                 font-family: helvetica, arial, sans-serif;
               }
       H1      { color: #000080;
                 font-family: helvetica, arial, sans-serif;
                 font-size: 22pt;
                 margin-left: 0in
               }
       H2      { color: #000080;  font: bold x-large helvetica, sans-serif;
                 margin-left: 0in }
       H3      { color: #000080;  font: bold   large helvetica, sans-serif; }
       H4      { color: #000080;  font: italic large helvetica, sans-serif; }
       .ruby   { background: #fff0f0 }
       .header { color: white }
       .subheader { color: #ffdddd }
       .sidebar   { width: 6in }
       span.sans { font-family: helvetica, arial, sans-serif }
       -->
   </STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="rdtool.html">Previous &lt;</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="support.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
    Copyright (c) 2001 by Addison Wesley Longman.  This
    material may be distributed only subject to the terms and
    conditions set forth in the Open Publication License, v1.0 or
    later (the latest version is presently available at
    http://www.opencontent.org/openpub/).
-->

<h1>Interactive Ruby Shell</h1><hr><br>
<P></P>
Back on page 126 we introduced irb, a Ruby module that lets you 
enter Ruby programs interactively and see the results
immediately. This appendix goes into more detail on using and
customizing irb.
<P></P>
<h2>Command Line</h2>
<P></P>
<code>irb</code> is run from the command line.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500"><tr><td><pre>
irb <i>[</i><i>irb-options</i><i>]</i> <i>[</i><i>ruby_script</i><i>]</i> <i>[</i><i>options</i><i>]</i>
</pre></td></tr></table>
<P></P>
The command-line options for irb are listed in Table
B.1 on page 524. Typically, you'll run irb with no options, but
if you want to run a script and watch the blow-by-blow description as
it runs, you can provide the name of the Ruby script and any options
for that script.
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>irb command-line options</b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
  <td valign="top"><b>Option</b></td>
  <td valign="top"><b>Description</b></td>
</tr>
<tr>
  <td valign="top"><code>-f</code></td>
  <td valign="top">Suppress reading <code>~/.irbrc</code>.</td>
</tr>
<tr>
  <td valign="top"><code>-m</code></td>
  <td valign="top">Math mode (fraction and matrix 
                          support is available).</td>
</tr>
<tr>
  <td valign="top"><code>-d</code></td>
  <td valign="top">Set <code>$DEBUG</code>  to true (same as
                          ``<code>ruby -d</code>'').</td>
</tr>
<tr>
  <td valign="top"><code>-r load-module</code></td>
  <td valign="top">Same as ``<code>ruby -r</code>''.</td>
</tr>
<tr>
  <td valign="top"><code>--inspect</code></td>
  <td valign="top">Use ``inspect'' for output (the default, unless
                          in math mode).</td>
</tr>
<tr>
  <td valign="top"><code>--noinspect</code></td>
  <td valign="top">Do not use inspect for output.</td>
</tr>
<tr>
  <td valign="top"><code>--readline</code></td>
  <td valign="top">Use <code>Readline</code> extension module.</td>
</tr>
<tr>
  <td valign="top"><code>--noreadline</code></td>
  <td valign="top">Do not use <code>Readline</code> extension module.</td>
</tr>
<tr>
  <td valign="top"><code>--prompt</code> <em>prompt-mode</em></td>
  <td valign="top">Switch prompt mode. Predefined
                         prompt modes are ``default'', ``simple'', 
                         ``xmp'', and ``inf-ruby''.</td>
</tr>
<tr>
  <td valign="top"><code>--prompt-mode</code> <em>prompt-mode</em></td>
  <td valign="top">Same as <code>--prompt</code>.</td>
</tr>
<tr>
  <td valign="top"><code>--inf-ruby-mode</code></td>
  <td valign="top">Sets up irb to run in <code>inf-ruby-mode</code>
                          under Emacs. Changes the prompt and
                          suppresses
                          <code>--readline</code>.</td>
</tr>
<tr>
  <td valign="top"><code>--simple-prompt</code></td>
  <td valign="top">Simple prompt mode.</td>
</tr>
<tr>
  <td valign="top"><code>--noprompt</code></td>
  <td valign="top">Do not display a prompt.</td>
</tr>
<tr>
  <td valign="top"><code>--tracer</code></td>
  <td valign="top">Display trace for execution of commands.</td>
</tr>
<tr>
  <td valign="top"><code>--back-trace-limit n</code></td>
  <td valign="top">Display backtrace information using
                          the top n and last n entries. 
                          The default value is 16.</td>
</tr>
<tr>
  <td valign="top"><code>--irb_debug n</code></td>
  <td valign="top">Set internal debug level to n (only for
                            irb development).</td>
</tr>
<tr>
  <td valign="top"><code>-v, --version</code></td>
  <td valign="top">Print the version of irb.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
<h2>Initialization File</h2>
<P></P>
irb uses an initialization file in which you can set commonly used
options or execute any required Ruby statements.  When irb is run, it
will try to load an initialization file from one of the following
sources in order: <code>~/.irbrc</code>, <code>.irbrc</code>, <code>irb.rc</code>,
<code>_irbrc</code>, and <code>$irbrc</code>.
<P></P>
Within the initialization file you may run any arbitrary Ruby code.
You can also set any of the configuration values that correspond to
command-line arguments as shown in Table B.2 on page 524.
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>irb configuration values</b>
<center>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<P></P>
<tr><td colspan="9" bgcolor="#ff9999" height="3"><img src="dot.gif" width="1" height="1"></td></tr><tr>
  <td valign="top"><code>IRB.conf[:IRB_NAME] = "irb"</code></td>
  <td valign="top"><code>IRB.conf[:MATH_MODE] = false</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:USE_TRACER] = false</code></td>
  <td valign="top"><code>IRB.conf[:USE_LOADER] = false</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:IGNORE_SIGINT] = true</code></td>
  <td valign="top"><code>IRB.conf[:IGNORE_EOF] = false</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:INSPECT_MODE] = nil</code></td>
  <td valign="top"><code>IRB.conf[:IRB_RC] = nil</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:BACK_TRACE_LIMIT] = 16</code></td>
  <td valign="top"><code>IRB.conf[:USE_LOADER] = false</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:USE_READLINE] = nil</code></td>
  <td valign="top"><code>IRB.conf[:USE_TRACER] = false</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:IGNORE_SIGINT] = true</code></td>
  <td valign="top"><code>IRB.conf[:IGNORE_EOF] = false</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:PROMPT_MODE] = :DEFAULT</code></td>
  <td valign="top"><code>IRB.conf[:PROMPT] = { ...&nbsp;}</code></td>
</tr>
<tr>
  <td valign="top"><code>IRB.conf[:DEBUG_LEVEL] = 0</code></td>
  <td valign="top"><code>IRB.conf[:VERBOSE] = true</code></td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</center>
</td></tr></table>
<P></P>
As an interesting twist on configuring irb, you can set
<code>IRB.conf[:IRB_RC]</code> to a <code>Proc</code> object.  This proc will be
invoked whenever the irb context is changed, and will receive that new
context as a parameter.  You can use this facility to change the
configuration dynamically based on the context.
<h2>Commands</h2>
<P></P>
At the irb prompt, you can enter any valid Ruby expression and see the
results.  You can also use any of the following commands to control
the irb session.
<P></P>
<dl>
<P></P>
  <dt><code>exit, quit, irb_exit</code></dt><dd>
    Quits this irb session or subsession.
    If you've used <code>cb</code> to change bindings (see below), exits from 
    this binding mode.
<P></P>
  </dd><dt><code>conf, irb_context</code></dt><dd>
    Displays current configuration. Modifying the configuration is
    achieved by invoking methods of <code>conf</code>.
<P></P>
  </dd><dt><code>conf.back_trace_limit n</code></dt><dd>
    Sets display lines of backtrace as top n and tail n. 
    The default value is 16.
<P></P>
  </dd><dt><code>conf.debug_level = N</code></dt><dd>
    Sets debug level of irb. 
<P></P>
  </dd><dt><code>conf.ignore_eof = true/false</code></dt><dd>
    Specifies the behavior of an end of file received on input.
    If true, it will be ignored; otherwise, it will quit irb.
<P></P>
  </dd><dt><code>conf.ignore_sigint= true/false</code></dt><dd>
    Specifies the behavior of ^C (control-c).  
    If false, ^C will quit irb. If true, ^C
    during input will cancel input and return to the top level;
    during execution, ^C will abort the current operation.
<P></P>
  </dd><dt><code>conf.inf_ruby_mode = true/false</code></dt><dd>
    If <code>true</code>, changes the prompt and disables readline support,
    allowing irb to work with
    <code>inf-ruby-mode</code>.    <em>[<code>inf-ruby-mode</code> allows Emacs      
      users to interact with
      Ruby while editing programs. See the file <code>inf_ruby.el</code> in the
      <code>misc</code> directory of the distribution for more details.]</em>  The
    default value is false.
<P></P>
  </dd><dt><code>conf.inspect_mode = true/false/nil</code></dt><dd>
    Specifies inspect mode according to the following values:
<P></P>
    <table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr>
  <td valign="top"><code>true</code></td>
  <td valign="top">Display inspect (default).</td>
</tr>
<tr>
  <td valign="top"><code>false</code></td>
  <td valign="top">Display to_s.</td>
</tr>
<tr>
  <td valign="top"><code>nil</code></td>
  <td valign="top">Inspect mode in non-math mode, 
            non-inspect mode in math mode.</td>
</tr>
</table>
<P></P>
  </dd><dt><code>conf.irb_level</code></dt><dd>
    Displays the current binding level (see <code>cb</code>).
<P></P>
  </dd><dt><code>conf.math_mode</code></dt><dd>
    Displays whether or not Ruby is in math mode.
<P></P>
  </dd><dt><code>conf.use_loader = true/false</code></dt><dd>
    Specifies whether or not irb's own file reader method is used with
    <code>load</code>/<code>require</code>. 
<P></P>
  </dd><dt><code>conf.prompt_c</code></dt><dd> 
    The prompt for a continuing statement
    (for example, immediately after an ``if'').
<P></P>
  </dd><dt><code>conf.prompt_i</code></dt><dd>
    The standard, top-level prompt.
<P></P>
  </dd><dt><code>conf.prompt_s</code></dt><dd>
    The prompt for a continuing string.
<P></P>
  </dd><dt><code>conf.rc = true/false</code></dt><dd>
    Specifies whether or not to use the initialization file
    <code>~/.irbrc</code>.
<P></P>
  </dd><dt><code>conf.use_prompt = true/false</code></dt><dd>
    Specifies whether or not to display prompts.
<P></P>
  </dd><dt><code>conf.use_readline = true/false/nil</code></dt><dd>
    Specifies whether or not to use Readline according to the following
    values:
<P></P>
    <table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr>
  <td valign="top"><code>true</code></td>
  <td valign="top">Use Readline.</td>
</tr>
<tr>
  <td valign="top"><code>false</code></td>
  <td valign="top">Do not use Readline.</td>
</tr>
<tr>
  <td valign="top"><code>nil</code></td>
  <td valign="top">Use Readline except for <code>inf-ruby-mode</code> (default).</td>
</tr>
</table>
<P></P>
  </dd><dt><code>conf.verbose=true/false</code></dt><dd>
    Specifies whether or not verbose messages are displayed. 
<P></P>
  </dd><dt><code>cb, irb_change_binding <i>[</i><i>obj</i><i>]</i></code></dt><dd>
    Creates and enters a new binding that has its own scope for
    local variables. If <i>obj</i> is given, it will be used as self in 
    the new binding.
<P></P>
  </dd><dt><code>irb <i>[</i>obj<i>]</i></code></dt><dd>
    Starts an irb subsession. If <i>obj</i> is given, it will be used
    as self.
<P></P>
  </dd><dt><code>jobs, irb_jobs</code></dt><dd>
    Lists irb subsessions.
<P></P>
  </dd><dt><code>fg n, irb_fg n</code></dt><dd>
    Switches into the specified irb subsession. n may be any of the
    following values:
<P></P>
    <table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr>
  <td valign="top">irb subsession number</td>
</tr>
<tr>
  <td valign="top">thread id</td>
</tr>
<tr>
  <td valign="top">irb object</td>
</tr>
<tr>
  <td valign="top">self (the <i>obj</i> that launched a particular subsession)</td>
</tr>
</table>
<P></P>
  </dd><dt><code>kill n, irb_kill n</code></dt><dd>
    Kills an irb subsession. n may be any of the values as described 
    for <code>irb_fg</code>.
<P></P>
</dd></dl>
<h3>Configuring the Prompt</h3>
<P></P>
You have a lot of flexibility in configuring the prompts that irb
uses.  Sets of prompts are stored in the prompt hash:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
IRB.conf[:PROMPT]
</pre></td></tr></table>

<P></P>
For example, to establish a new prompt mode called ``MY_PROMPT'', you
might enter the following (either directly at an irb prompt or in the 
<code>.irbrc</code> file):
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
IRB.conf[:PROMPT][:MY_PROMPT]&nbsp;=&nbsp;{&nbsp;#&nbsp;name&nbsp;of&nbsp;prompt&nbsp;mode
&nbsp;&nbsp;:PROMPT_I&nbsp;=>&nbsp;"...",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;normal&nbsp;prompt
&nbsp;&nbsp;:PROMPT_S&nbsp;=>&nbsp;"...",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;prompt&nbsp;for&nbsp;continuing&nbsp;strings
&nbsp;&nbsp;:PROMPT_C&nbsp;=>&nbsp;"...",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;prompt&nbsp;for&nbsp;continuing&nbsp;statement
&nbsp;&nbsp;:RETURN&nbsp;=>&nbsp;"&nbsp;&nbsp;&nbsp;&nbsp;==>%s\n"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;format&nbsp;to&nbsp;return&nbsp;value
}
</pre></td></tr></table>

<P></P>
Then, invoke irb with the prompt mode above by
<P></P>
<pre>
<code>%&nbsp;irb --prompt my-prompt</code>
</pre>
<P></P>
Or set the following configuration value:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
IRB.conf[:PROMPT_MODE]&nbsp;=&nbsp;:MY_PROMPT
</pre></td></tr></table>

<P></P>
The constants <code>PROMPT_I</code>, <code>PROMPT_S</code>, and <code>PROMPT_C</code>
specify the format for each of the prompt strings.  Within the prompt
format, the following flags are available and will expand to the
given text:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
  <td valign="top"><b>Flag</b></td>
  <td valign="top"><b>Description</b></td>
</tr>
<tr>
  <td valign="top"><code>%N</code></td>
  <td valign="top">Current command.</td>
</tr>
<tr>
  <td valign="top"><code>%m</code></td>
  <td valign="top"><code>to_s</code> of the main object (self).</td>
</tr>
<tr>
  <td valign="top"><code>%M</code></td>
  <td valign="top"><code>inspect</code> of the main object (self).</td>
</tr>
<tr>
  <td valign="top"><code>%l</code></td>
  <td valign="top">Delimiter type. In strings that are continued across a line break,
         <code>%l</code> will display the type of delimiter used to begin the
         string, so you'll know how to end it.  The delimiter will be
         one of <code>"</code>, <code>'</code>, <code>/</code>, <code>]</code>, or <code>`</code>.</td>
</tr>
<tr>
  <td valign="top"><code>%ni</code></td>
  <td valign="top">Indent level. The optional number n is used as a width 
            specification to printf, as <code>printf("%nd")</code>.</td>
</tr>
<tr>
  <td valign="top"><code>%nn</code></td>
  <td valign="top">Current line number (n used as with the indent level).</td>
</tr>
<tr>
  <td valign="top"><code>%%</code></td>
  <td valign="top">A literal percent sign.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
For instance, the default prompt mode is defined as follows:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
IRB.conf[:PROMPT_MODE][:DEFAULT]&nbsp;=&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:PROMPT_I&nbsp;=>&nbsp;"%N(%m):%03n:%i>&nbsp;",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:PROMPT_S&nbsp;=>&nbsp;"%N(%m):%03n:%i%l&nbsp;",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:PROMPT_C&nbsp;=>&nbsp;"%N(%m):%03n:%i*&nbsp;",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:RETURN&nbsp;=>&nbsp;"%s\n"
}
</pre></td></tr></table>

<h2>Restrictions</h2>
<P></P>
Because of the way irb works, there is a minor incompatibility between
it and the standard Ruby interpreter.  The problem lies in the
determination of local variables.
<P></P>
Normally, Ruby looks for an assignment statement to determine if
something is a variable---if a name hasn't been assigned to, then Ruby
assumes that name is a method call.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
eval&nbsp;"a&nbsp;=&nbsp;0"
a
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
prog.rb:2: undefined local variable or method `a'<br>for #&lt;Object:0x4019ac90> (NameError)
</pre></td></tr></table>

<P></P>
In this case, the assignment is there, but it's within a string, so
Ruby doesn't take it into account.
<P></P>
irb, on the other hand, executes statements as they are entered. 
<P></P>
<pre>
  irb(main):001:0> eval "a = 0"
  0
  irb(main):002:0> a
  0
</pre>
<P></P>
In irb, the assignment was executed before the second line was
encountered, so ``a'' is correctly identified as a local variable.
<P></P>
If you need to match the Ruby behavior more closely, you can place
these statements within a <code>begin</code>/<code>end</code> pair.
<P></P>
<pre>
  irb(main):001:0> begin
  irb(main):002:1*   eval "a = 0"
  irb(main):003:1>   a
  irb(main):004:1> end
  NameError: undefined local variable or method `a'
  (irb):3:in `irb_binding'
</pre>
<h2>rtags, xmp, and the Frame Class</h2>
<P></P>
The base version of irb is installed along with Ruby itself.  But
there is an extended version of irb in the archives containing a
few extra goodies that need mentioning.
<h3>rtags</h3>
<P></P>
<code>rtags</code> is a command used to create a <code>TAGS</code> file for use with
either the emacs or vi editor.  
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500"><tr><td><pre>
rtags <i>[</i><i>-vi</i><i>]</i> <i>[</i><i>files</i><i>]</i>...</pre></td></tr></table>
<P></P>
By default, rtags makes a <code>TAGS</code> file suitable for emacs (see
etags.el). The <code>-vi</code> option makes a TAGS file for use with vi.
<P></P>
rtags needs to be installed in the same manner as irb (that is, you
need to install irb in the library path and make a link from
<code>irb/rtags.rb</code> to <code>bin/rtags</code>).
<h3>xmp</h3>
<P></P>
irb's xmp is an ``example printer''---that is, a pretty-printer that
shows the value of each expression as it is run (much like the script
we wrote to format the examples in this book). There is also another
stand-alone xmp in the archives.
<P></P>
xmp can be used as follows:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;"irb/xmp"
<P></P>
xmp&nbsp;&lt;&lt;END
artist&nbsp;=&nbsp;"Doc&nbsp;Severinsen"
artist
END
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
artist&nbsp;=&nbsp;"Doc&nbsp;Severinsen"
&nbsp;&nbsp;&nbsp;&nbsp;==>"Doc&nbsp;Severinsen"
artist
&nbsp;&nbsp;&nbsp;&nbsp;==>"Doc&nbsp;Severinsen"
</pre></td></tr></table>

<P></P>
Or, it can be used as an object instance.  Used in this fashion, the
object maintains context between invocations:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;"irb/xmp"
<P></P>
x&nbsp;=&nbsp;XMP.new
x.puts&nbsp;&lt;&lt;END
artist&nbsp;=&nbsp;"Louis&nbsp;Prima"
END
<P></P>
x.puts&nbsp;&lt;&lt;END
artist
END
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
artist&nbsp;=&nbsp;"Louis&nbsp;Prima"
&nbsp;&nbsp;&nbsp;&nbsp;==>"Louis&nbsp;Prima"
artist
&nbsp;&nbsp;&nbsp;&nbsp;==>"Louis&nbsp;Prima"
</pre></td></tr></table>

<P></P>
You can explicitly provide a binding with either form; otherwise, xmp
uses the caller's environment.
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
xmp&nbsp;code_string,&nbsp;abinding
XMP.new(abinding)
</pre></td></tr></table>

<P></P>
Note that xmp does not work with multithreading.
<h3>The Frame Class</h3>
<P></P>
The <code>IRB::Frame</code> class represents the interpreter's stack and allows
easy access to the <code>Binding</code> environment in effect at different
stack levels. 
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<P></P>
  <tr><td colspan="9" bgcolor="#ff9999" height="3"><img src="dot.gif" width="1" height="1"></td></tr><tr>
  <td valign="top"><code>IRB::Frame.top(<em>n = 0</em>)</code></td>
  <td valign="top">Returns a <code>Binding</code>
  for the nth context from the top. The 0th context is
  topmost,
  most recent frame.</td>
</tr>
<tr>
  <td valign="top"><code>IRB::Frame.bottom(<em>n = 0</em>)</code></td>
  <td valign="top">Returns a <code>Binding</code> for the
  nth context from the bottom. The 0th context is the
  bottommost, initial frame.</td>
</tr>
<tr>
  <td valign="top"><code>IRB::Frame.sender</code></td>
  <td valign="top">Returns the object (the sender)
  that invoked the current method.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
You can use this facility, for instance, to examine local variables
from the method that called the current method:
<P></P>

<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require&nbsp;'irb/frame'
<P></P>
def&nbsp;outie
&nbsp;&nbsp;b&nbsp;=&nbsp;IRB::Frame.top(1)
&nbsp;&nbsp;eval&nbsp;"p&nbsp;my_local",&nbsp;b
end
<P></P>
def&nbsp;innie
&nbsp;&nbsp;my_local&nbsp;=&nbsp;102.7
&nbsp;&nbsp;outie
end
<P></P>
innie
</pre></td></tr></table>

<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
102.7
</pre></td></tr></table>

<P></P>
Note that this doesn't work with multithreaded programs.

<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="rdtool.html">Previous &lt;</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="support.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
     The Pragmatic Programmer's Guide"</font><br><font size="-3">
      Copyright
      &#169;
      2000 Addison Wesley Longman, Inc. Released under the terms of the
      <a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
        <br>
      This reference is available for
        <a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
   </font></body></html>