summaryrefslogtreecommitdiff
path: root/doc/irb/irb.rd
blob: a42cd46680674aef4d33cb11f75a55e69737b9d1 (plain)
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
irb -- interactive ruby
		$Release Version: 0.9 $
		$Revision$
		$Date$
		by Keiju ISHITSUKA(keiju@ishitsuka.com)
		by gotoken-san who is original translater from japanese version

=begin
= What is irb?

irb stands for `interactive ruby'. irb is a tool to execute interactively
ruby expressions read from stdin. 

= Invoking

  % irb

= Usage

Use of irb is easy if you know ruby.  Executing irb, prompts are 
displayed as follows. Then, enter expression of ruby. A input is
executed when it is syntacticaly completed. 

  dim% irb
  irb(main):001:0> 1+2
  3
  irb(main):002:0> class Foo
  irb(main):003:1>  def foo
  irb(main):004:2>    print 1
  irb(main):005:2>  end
  irb(main):006:1> end
  nil
  irb(main):007:0> 

And, Readline extesion module can be used with irb. Using Readline
is the standard default action if Readline is installed. 

= Command line option

  irb.rb [options] file_name opts
  options:
  -f		    suppress read ~/.irbrc 
  -m		    bc mode (fraction or matrix are available)
  -d                set $DEBUG  to true (same as `ruby -d')
  -Kc		    same as `ruby -Kc'
  -r load-module    same as `ruby -r'
  --verbose	    command input is echoed(default)
  --noverbose	    command input isn't echoed
  --echo	    commands are echoed immediately before execution(default)
  --noecho	    commands aren't echoed immediately before execution
  --inspect	    uses `inspect' for output (the default except bc mode)
  --noinspect	    doesn't uses inspect for output
  --readline	    uses Readline extension module
  --noreadline	    doesn't use Readline extension module
  --prompt prompt-mode
  --prompt-mode prompt-mode
		    switches prompt mode. Pre-defined prompt modes are
		    `default', `simple', `xmp' and `inf-ruby'
			    
  --inf-ruby-mode   uses prompt appreciate for inf-ruby-mode on emacs. 
		    Suppresses --readline. 
  --simple-prompt   simple prompt mode
  --noprompt	    no prompt
  --tracer	    display trace for each execution of commands.
  --back-trace-limit n
		    displayes backtrace top n and tail n. The default
		    value is 16. 
  --irb_debug n	    sets internal debug level to n (It shouldn't be used)
  -v, --version	    prints the version of irb

= Configurations

irb reads `~/.irbrc' when it is invoked. If `~/.irbrb' doesn't exist
irb try to read in the order `.irbrc', `irb.rc', `_irbrc' then `$irbrc'. 

The following is altanative to the command line option. To use them
type as follows in an irb session. 

  IRB.conf[:IRB_NAME]="irb"
  IRB.conf[:MATH_MODE]=false
  IRB.conf[:USE_TRACER]=false
  IRB.conf[:USE_LOADER]=false
  IRB.conf[:IGNORE_SIGINT]=true
  IRB.conf[:IGNORE_EOF]=false
  IRB.conf[:INSPECT_MODE]=nil
  IRB.conf[:IRB_RC] = nil
  IRB.conf[:BACK_TRACE_LIMIT]=16
  IRB.conf[:USE_LOADER] = false
  IRB.conf[:USE_READLINE] = nil
  IRB.conf[:USE_TRACER] = false
  IRB.conf[:IGNORE_SIGINT] = true
  IRB.conf[:IGNORE_EOF] = false
  IRB.conf[:PROMPT_MODE] = :DEFALUT
  IRB.conf[:PROMPT] = {...}
  IRB.conf[:DEBUG_LEVEL]=0
  IRB.conf[:VERBOSE]=true

== Customizing prompt

To costomize the prompt you set a variable

  IRB.conf[:PROMPT]

For example, describe as follows in `.irbrc'. 

  IRB.conf[:PROMPT][:MY_PROMPT] = { # name of prompt mode
    :PROMPT_I => nil,		  # normal prompt
    :PROMPT_S => nil,		  # prompt for continuated strings
    :PROMPT_C => nil,		  # prompt for continuated statement
    :RETURN => "    ==>%s\n"	  # format to return value
  }

Then, invoke irb with the above prompt mode by

  % irb --prompt my-prompt

Or add the following in `.irbrc'. 

  IRB.conf[:PROMPT_MODE] = :MY_PROMPT

Constants PROMPT_I, PROMPT_S and PROMPT_C specifies the format. 
In the prompt specification, some special strings are available. 

  %N	command name which is running
  %m	to_s of main object (self)
  %M	inspect of main object (self)
  %l	type of string(", ', /, ]), `]' is inner %w[...]
  %NNi	indent level. NN is degits and means as same as printf("%NNd"). 
        It can be ommited
  %NNn	line number. 
  %%    %

For instance, the default prompt mode is defined as follows:

IRB.conf[:PROMPT_MODE][:DEFAULT] = {
      :PROMPT_I => "%N(%m):%03n:%i> ",
      :PROMPT_S => "%N(%m):%03n:%i%l ",
      :PROMPT_C => "%N(%m):%03n:%i* ",
      :RETURN => "%s\n"
} 

RETURN is used to printf. 

== Configurating subirb

The command line option or IRB.conf specify the default behavior of
(sub)irb. On the other hand, each conf of in the next sction `6. Command' 
is used to individually configurate (sub)irb. 

If proc is set to IRB.conf[:IRB_RC], its subirb will be invoked after
execution of that proc under giving the context of irb as its
aregument. By this mechanism each subirb can be configurated. 

= Command

For irb commands, both simple name and `irb_'-prefixed name are prepared. 

--- exit, quit, irb_exit	
    Quits (sub)irb. 

--- conf, irb_context
    Displays current configuration. Modifing the configuration is
    achieved by sending message to `conf'. 

--- conf.eval_history = N
    Sets execution result history.
    N is a integer or nil. If N > 0, the number of historys is N. 
    If N == 0, the number of historys is unlimited. If N is nill,
    execution result history isn't used(default).

--- conf.back_trace_limit
    Sets display lines of backtrace as top n and tail n. 
    The default value is 16.
    
--- conf.debug_level = N
    Sets debug level of irb. 

--- conf.ignore_eof = true/false
    Whether ^D (control-d) will be ignored or not. 
    If false is set, ^D means quit. 

--- conf.ignore_sigint= true/false
    Whether ^C (control-c) will be ignored or not. 
    If false is set, ^D means quit.  If true, 
      during input:   cancel inputing then return to top level. 
      during execute: abondon current execution. 

--- conf.inf_ruby_mode = true/false
    Whether inf-ruby-mode or not. The default value is false.

--- conf.inspect_mode = true/false/nil
    Specifies inspect mode. 
    true:  display inspect
    false: display to_s
    nil:   inspect mode in non math mode, 
           non inspect mode in math mode. 

--- conf.math_mode
    Whether bc mode or not. 

--- conf.use_loader = true/false
    Whether irb's own file reader method is used when load/require or not. 
    This mode is globaly affected (irb wide). 

--- conf.prompt_c
    prompt for a continuating statement (e.g, immediately after of `if')

--- conf.prompt_i
    standard prompt

--- conf.prompt_s
    prompt for a continuating string

--- conf.rc
    Whether ~/.irbrc is read or not. 

--- conf.use_prompt = true/false
    Prompting or not. 

--- conf.use_readline = true/false/nil
    Whether readline is used or not. 
    true: uses 
    false: doen't use
    nil: intends to use readline except for inf-ruby-mode (default)
#
#--- conf.verbose=T/F
#    Whether verbose messages are display or not. 

--- cws, chws, irb_change_workspace [obj]
    obj will be self. If obj is omitted, self will be home-object, or
    the main object of first started irb.

--- pushws, irb_pushws, irb_push_workspace [obj]
    same as UNIX-shell command pushd.

--- popws, irb_popws, irb_pop_workspace
    same as UNIX-shell command popd

--- irb [obj]
    Invoke subirb. If obj is given, obj will be self. 

--- jobs, irb_jobs
    List of subirb

--- fg n, irb_fg n
    Switch into specified subirb. The following is candidates of n:

      irb number
      thhread
      irb object
      self(obj which is specified of irb obj)

--- kill n, irb_kill n
    Kill subirb. The means of n is as same as the case of irb_fg. 

--- souce, irb_source  path
    This is a like UNIX-shell command source. evaluate script in path
    on current context.

--- irb_load path, prev
    irb-version of Ruby's load.

= System variable

--- _  The latest value of evaluation (it is local)
--- __ The history of evaluation values.
    __[line_no] return an evaluation value of line number<line_no>. If
    line_no is a negative, return value before -<line_no> from latest
    value.

= Session Example

  dim% ruby irb.rb
  irb(main):001:0> irb                        # invoke subirb
  irb#1(main):001:0> jobs                     # list of subirbs
  #0->irb on main (#<Thread:0x400fb7e4> : stop)
  #1->irb#1 on main (#<Thread:0x40125d64> : running)
  nil
  irb#1(main):002:0> fg 0                     # switch job
  nil
  irb(main):002:0> class Foo;end
  nil
  irb(main):003:0> irb Foo                    # invoke subirb which has the 
					      #              context of Foo
  irb#2(Foo):001:0> def foo                   # define Foo#foo
  irb#2(Foo):002:1>   print 1
  irb#2(Foo):003:1> end
  nil
  irb#2(Foo):004:0> fg 0                      # switch job
  nil
  irb(main):004:0> jobs                       # list of job
  #0->irb on main (#<Thread:0x400fb7e4> : running)
  #1->irb#1 on main (#<Thread:0x40125d64> : stop)
  #2->irb#2 on Foo (#<Thread:0x4011d54c> : stop)
  nil
  irb(main):005:0> Foo.instance_methods       # Foo#foo is defined asurely
  ["foo"]
  irb(main):006:0> fg 2                       # switch job
  nil
  irb#2(Foo):005:0> def bar                   # define Foo#bar
  irb#2(Foo):006:1>  print "bar"
  irb#2(Foo):007:1> end
  nil
  irb#2(Foo):010:0>  Foo.instance_methods
  ["bar", "foo"]
  irb#2(Foo):011:0> fg 0                      
  nil
  irb(main):007:0> f = Foo.new
  #<Foo:0x4010af3c>
  irb(main):008:0> irb f                      # invoke subirb which has the
					      #  context of f (instance of Foo)
  irb#3(#<Foo:0x4010af3c>):001:0> jobs
  #0->irb on main (#<Thread:0x400fb7e4> : stop)
  #1->irb#1 on main (#<Thread:0x40125d64> : stop)
  #2->irb#2 on Foo (#<Thread:0x4011d54c> : stop)
  #3->irb#3 on #<Foo:0x4010af3c> (#<Thread:0x4010a1e0> : running)
  nil
  irb#3(#<Foo:0x4010af3c>):002:0> foo         # evaluate f.foo
  1nil
  irb#3(#<Foo:0x4010af3c>):003:0> bar         # evaluate f.bar
  barnil
  irb#3(#<Foo:0x4010af3c>):004:0> kill 1, 2, 3# kill job
  nil
  irb(main):009:0> jobs
  #0->irb on main (#<Thread:0x400fb7e4> : running)
  nil
  irb(main):010:0> exit                       # exit
  dim% 

= Restrictions

Because irb evaluates the inputs immediately after the imput is
syntactically completed, irb gives slight different result than
directly use ruby. Known difference is pointed out here. 


== Declaration of the local variable

The following causes an error in ruby:

  eval "foo = 0"
  foo
  --
  -:2: undefined local variable or method `foo' for #<Object:0x40283118> (NameError)
  ---
  NameError

Though, the above will successfully done by irb. 

  >> eval "foo = 0"
 => 0
 >> foo
 => 0

Ruby evaluates a code after reading entire of code and determination
of the scope of local variables. On the other hand, irb do
immediately. More precisely, irb evaluate at first

  evel "foo = 0" 

then foo is defined on this timing. It is because of this
incompatibility.

If you'd like to detect those differences, begin...end can be used:

  >> begin
  ?>   eval "foo = 0"
  >>   foo
  >> end
  NameError: undefined local variable or method `foo' for #<Object:0x4013d0f0>
  (irb):3
  (irb_local_binding):1:in `eval'

== Here-document

Implementation of Here-document is incomplete. 

== Symbol

Irb can not always recognize a symbol as to be Symbol. Concretely, an
expression have completed, however Irb regard it as continuation line.

=end

% Begin Emacs Environment
% Local Variables:
% mode: text
% comment-column: 0
% comment-start: "%"
% comment-end: "\n"
% End:
%