irb 1.0.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.document +4 -0
  3. data/Gemfile +10 -2
  4. data/LICENSE.txt +3 -3
  5. data/README.md +3 -3
  6. data/Rakefile +17 -1
  7. data/doc/irb/irb-tools.rd.ja +184 -0
  8. data/doc/irb/irb.rd.ja +427 -0
  9. data/irb.gemspec +18 -4
  10. data/lib/irb/cmd/fork.rb +2 -4
  11. data/lib/irb/cmd/help.rb +10 -5
  12. data/lib/irb/cmd/info.rb +32 -0
  13. data/lib/irb/cmd/ls.rb +101 -0
  14. data/lib/irb/cmd/measure.rb +43 -0
  15. data/lib/irb/cmd/nop.rb +10 -4
  16. data/lib/irb/cmd/pushws.rb +0 -1
  17. data/lib/irb/cmd/show_source.rb +93 -0
  18. data/lib/irb/cmd/whereami.rb +20 -0
  19. data/lib/irb/color.rb +246 -0
  20. data/lib/irb/color_printer.rb +47 -0
  21. data/lib/irb/completion.rb +254 -55
  22. data/lib/irb/context.rb +165 -72
  23. data/lib/irb/easter-egg.rb +138 -0
  24. data/lib/irb/ext/change-ws.rb +0 -1
  25. data/lib/irb/ext/history.rb +47 -11
  26. data/lib/irb/ext/loader.rb +46 -20
  27. data/lib/irb/ext/multi-irb.rb +7 -7
  28. data/lib/irb/ext/save-history.rb +36 -11
  29. data/lib/irb/ext/tracer.rb +14 -2
  30. data/lib/irb/ext/use-loader.rb +4 -3
  31. data/lib/irb/ext/workspaces.rb +0 -1
  32. data/lib/irb/extend-command.rb +113 -63
  33. data/lib/irb/frame.rb +12 -7
  34. data/lib/irb/help.rb +0 -1
  35. data/lib/irb/init.rb +146 -26
  36. data/lib/irb/input-method.rb +287 -9
  37. data/lib/irb/inspector.rb +15 -11
  38. data/lib/irb/lc/error.rb +55 -16
  39. data/lib/irb/lc/help-message +25 -13
  40. data/lib/irb/lc/ja/error.rb +55 -14
  41. data/lib/irb/lc/ja/help-message +11 -6
  42. data/lib/irb/locale.rb +13 -4
  43. data/lib/irb/notifier.rb +12 -8
  44. data/lib/irb/output-method.rb +6 -6
  45. data/lib/irb/ruby-lex.rb +673 -992
  46. data/lib/irb/ruby_logo.aa +37 -0
  47. data/lib/irb/version.rb +2 -2
  48. data/lib/irb/workspace.rb +65 -21
  49. data/lib/irb/xmp.rb +1 -1
  50. data/lib/irb.rb +276 -96
  51. data/man/irb.1 +229 -0
  52. metadata +25 -31
  53. data/.gitignore +0 -9
  54. data/.travis.yml +0 -6
  55. data/lib/irb/lc/.document +0 -4
  56. data/lib/irb/ruby-token.rb +0 -267
  57. data/lib/irb/slex.rb +0 -282
data/lib/irb/context.rb CHANGED
@@ -22,10 +22,10 @@ module IRB
22
22
  #
23
23
  # The optional +input_method+ argument:
24
24
  #
25
- # +nil+:: uses stdin or Readline
25
+ # +nil+:: uses stdin or Reidline or Readline
26
26
  # +String+:: uses a File
27
27
  # +other+:: uses this as InputMethod
28
- def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
28
+ def initialize(irb, workspace = nil, input_method = nil)
29
29
  @irb = irb
30
30
  if workspace
31
31
  @workspace = workspace
@@ -39,7 +39,22 @@ module IRB
39
39
  @rc = IRB.conf[:RC]
40
40
  @load_modules = IRB.conf[:LOAD_MODULES]
41
41
 
42
- @use_readline = IRB.conf[:USE_READLINE]
42
+ if IRB.conf.has_key?(:USE_SINGLELINE)
43
+ @use_singleline = IRB.conf[:USE_SINGLELINE]
44
+ elsif IRB.conf.has_key?(:USE_READLINE) # backward compatibility
45
+ @use_singleline = IRB.conf[:USE_READLINE]
46
+ else
47
+ @use_singleline = nil
48
+ end
49
+ if IRB.conf.has_key?(:USE_MULTILINE)
50
+ @use_multiline = IRB.conf[:USE_MULTILINE]
51
+ elsif IRB.conf.has_key?(:USE_REIDLINE) # backward compatibility
52
+ @use_multiline = IRB.conf[:USE_REIDLINE]
53
+ else
54
+ @use_multiline = nil
55
+ end
56
+ @use_colorize = IRB.conf[:USE_COLORIZE]
57
+ @use_autocomplete = IRB.conf[:USE_AUTOCOMPLETE]
43
58
  @verbose = IRB.conf[:VERBOSE]
44
59
  @io = nil
45
60
 
@@ -64,23 +79,42 @@ module IRB
64
79
 
65
80
  case input_method
66
81
  when nil
67
- case use_readline?
82
+ @io = nil
83
+ case use_multiline?
68
84
  when nil
69
- if (defined?(ReadlineInputMethod) && STDIN.tty? &&
70
- IRB.conf[:PROMPT_MODE] != :INF_RUBY)
71
- @io = ReadlineInputMethod.new
85
+ if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline?
86
+ # Both of multiline mode and singleline mode aren't specified.
87
+ @io = ReidlineInputMethod.new
72
88
  else
73
- @io = StdioInputMethod.new
89
+ @io = nil
74
90
  end
75
91
  when false
76
- @io = StdioInputMethod.new
92
+ @io = nil
77
93
  when true
78
- if defined?(ReadlineInputMethod)
79
- @io = ReadlineInputMethod.new
94
+ @io = ReidlineInputMethod.new
95
+ end
96
+ unless @io
97
+ case use_singleline?
98
+ when nil
99
+ if (defined?(ReadlineInputMethod) && STDIN.tty? &&
100
+ IRB.conf[:PROMPT_MODE] != :INF_RUBY)
101
+ @io = ReadlineInputMethod.new
102
+ else
103
+ @io = nil
104
+ end
105
+ when false
106
+ @io = nil
107
+ when true
108
+ if defined?(ReadlineInputMethod)
109
+ @io = ReadlineInputMethod.new
110
+ else
111
+ @io = nil
112
+ end
80
113
  else
81
- @io = StdioInputMethod.new
114
+ @io = nil
82
115
  end
83
116
  end
117
+ @io = StdioInputMethod.new unless @io
84
118
 
85
119
  when String
86
120
  @io = FileInputMethod.new(input_method)
@@ -91,17 +125,22 @@ module IRB
91
125
  end
92
126
  self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
93
127
 
94
- if output_method
95
- @output_method = output_method
96
- else
97
- @output_method = StdioOutputMethod.new
98
- end
128
+ @extra_doc_dirs = IRB.conf[:EXTRA_DOC_DIRS]
99
129
 
100
130
  @echo = IRB.conf[:ECHO]
101
131
  if @echo.nil?
102
132
  @echo = true
103
133
  end
104
- self.debug_level = IRB.conf[:DEBUG_LEVEL]
134
+
135
+ @echo_on_assignment = IRB.conf[:ECHO_ON_ASSIGNMENT]
136
+ if @echo_on_assignment.nil?
137
+ @echo_on_assignment = :truncate
138
+ end
139
+
140
+ @newline_before_multiline_output = IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]
141
+ if @newline_before_multiline_output.nil?
142
+ @newline_before_multiline_output = true
143
+ end
105
144
  end
106
145
 
107
146
  # The top-level workspace, see WorkSpace#main
@@ -111,18 +150,18 @@ module IRB
111
150
 
112
151
  # The toplevel workspace, see #home_workspace
113
152
  attr_reader :workspace_home
114
- # WorkSpace in the current context
153
+ # WorkSpace in the current context.
115
154
  attr_accessor :workspace
116
- # The current thread in this context
155
+ # The current thread in this context.
117
156
  attr_reader :thread
118
- # The current input method
157
+ # The current input method.
119
158
  #
120
- # Can be either StdioInputMethod, ReadlineInputMethod, FileInputMethod or
121
- # other specified when the context is created. See ::new for more
122
- # information on +input_method+.
159
+ # Can be either StdioInputMethod, ReadlineInputMethod,
160
+ # ReidlineInputMethod, FileInputMethod or other specified when the
161
+ # context is created. See ::new for more # information on +input_method+.
123
162
  attr_accessor :io
124
163
 
125
- # Current irb session
164
+ # Current irb session.
126
165
  attr_accessor :irb
127
166
  # A copy of the default <code>IRB.conf[:AP_NAME]</code>
128
167
  attr_accessor :ap_name
@@ -137,26 +176,34 @@ module IRB
137
176
  # +input_method+ passed to Context.new
138
177
  attr_accessor :irb_path
139
178
 
140
- # Whether +Readline+ is enabled or not.
179
+ # Whether multiline editor mode is enabled or not.
141
180
  #
142
- # A copy of the default <code>IRB.conf[:USE_READLINE]</code>
181
+ # A copy of the default <code>IRB.conf[:USE_MULTILINE]</code>
182
+ attr_reader :use_multiline
183
+ # Whether singleline editor mode is enabled or not.
143
184
  #
144
- # See #use_readline= for more information.
145
- attr_reader :use_readline
185
+ # A copy of the default <code>IRB.conf[:USE_SINGLELINE]</code>
186
+ attr_reader :use_singleline
187
+ # Whether colorization is enabled or not.
188
+ #
189
+ # A copy of the default <code>IRB.conf[:USE_COLORIZE]</code>
190
+ attr_reader :use_colorize
191
+ # A copy of the default <code>IRB.conf[:USE_AUTOCOMPLETE]</code>
192
+ attr_reader :use_autocomplete
146
193
  # A copy of the default <code>IRB.conf[:INSPECT_MODE]</code>
147
194
  attr_reader :inspect_mode
148
195
 
149
196
  # A copy of the default <code>IRB.conf[:PROMPT_MODE]</code>
150
197
  attr_reader :prompt_mode
151
- # Standard IRB prompt
198
+ # Standard IRB prompt.
152
199
  #
153
200
  # See IRB@Customizing+the+IRB+Prompt for more information.
154
201
  attr_accessor :prompt_i
155
- # IRB prompt for continuated strings
202
+ # IRB prompt for continuated strings.
156
203
  #
157
204
  # See IRB@Customizing+the+IRB+Prompt for more information.
158
205
  attr_accessor :prompt_s
159
- # IRB prompt for continuated statement (e.g. immediately after an +if+)
206
+ # IRB prompt for continuated statement. (e.g. immediately after an +if+)
160
207
  #
161
208
  # See IRB@Customizing+the+IRB+Prompt for more information.
162
209
  attr_accessor :prompt_c
@@ -165,17 +212,17 @@ module IRB
165
212
  # Can be either the default <code>IRB.conf[:AUTO_INDENT]</code>, or the
166
213
  # mode set by #prompt_mode=
167
214
  #
168
- # To enable auto-indentation in irb:
215
+ # To disable auto-indentation in irb:
169
216
  #
170
- # IRB.conf[:AUTO_INDENT] = true
217
+ # IRB.conf[:AUTO_INDENT] = false
171
218
  #
172
219
  # or
173
220
  #
174
- # irb_context.auto_indent_mode = true
221
+ # irb_context.auto_indent_mode = false
175
222
  #
176
223
  # or
177
224
  #
178
- # IRB.CurrentContext.auto_indent_mode = true
225
+ # IRB.CurrentContext.auto_indent_mode = false
179
226
  #
180
227
  # See IRB@Configuration for more information.
181
228
  attr_accessor :auto_indent_mode
@@ -196,9 +243,12 @@ module IRB
196
243
  #
197
244
  # If set to +false+, <code>^D</code> will quit irb.
198
245
  attr_accessor :ignore_eof
246
+ # Specify the installation locations of the ri file to be displayed in the
247
+ # document dialog.
248
+ attr_accessor :extra_doc_dirs
199
249
  # Whether to echo the return value to output or not.
200
250
  #
201
- # Uses IRB.conf[:ECHO] if available, or defaults to +true+.
251
+ # Uses <code>IRB.conf[:ECHO]</code> if available, or defaults to +true+.
202
252
  #
203
253
  # puts "hello"
204
254
  # # hello
@@ -207,14 +257,59 @@ module IRB
207
257
  # puts "omg"
208
258
  # # omg
209
259
  attr_accessor :echo
260
+ # Whether to echo for assignment expressions.
261
+ #
262
+ # If set to +false+, the value of assignment will not be shown.
263
+ #
264
+ # If set to +true+, the value of assignment will be shown.
265
+ #
266
+ # If set to +:truncate+, the value of assignment will be shown and truncated.
267
+ #
268
+ # It defaults to +:truncate+.
269
+ #
270
+ # a = "omg"
271
+ # #=> omg
272
+ #
273
+ # a = "omg" * 10
274
+ # #=> omgomgomgomgomgomgomg...
275
+ #
276
+ # IRB.CurrentContext.echo_on_assignment = false
277
+ # a = "omg"
278
+ #
279
+ # IRB.CurrentContext.echo_on_assignment = true
280
+ # a = "omg" * 10
281
+ # #=> omgomgomgomgomgomgomgomgomgomg
282
+ #
283
+ # To set the behaviour of showing on assignment in irb:
284
+ #
285
+ # IRB.conf[:ECHO_ON_ASSIGNMENT] = :truncate or true or false
286
+ #
287
+ # or
288
+ #
289
+ # irb_context.echo_on_assignment = :truncate or true or false
290
+ #
291
+ # or
292
+ #
293
+ # IRB.CurrentContext.echo_on_assignment = :truncate or true or false
294
+ attr_accessor :echo_on_assignment
295
+ # Whether a newline is put before multiline output.
296
+ #
297
+ # Uses <code>IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT]</code> if available,
298
+ # or defaults to +true+.
299
+ #
300
+ # "abc\ndef"
301
+ # #=>
302
+ # abc
303
+ # def
304
+ # IRB.CurrentContext.newline_before_multiline_output = false
305
+ # "abc\ndef"
306
+ # #=> abc
307
+ # def
308
+ attr_accessor :newline_before_multiline_output
210
309
  # Whether verbose messages are displayed or not.
211
310
  #
212
311
  # A copy of the default <code>IRB.conf[:VERBOSE]</code>
213
312
  attr_accessor :verbose
214
- # The debug level of irb
215
- #
216
- # See #debug_level= for more information.
217
- attr_reader :debug_level
218
313
 
219
314
  # The limit of backtrace lines displayed as top +n+ and tail +n+.
220
315
  #
@@ -225,18 +320,36 @@ module IRB
225
320
  # See IRB@Command+line+options for more command line options.
226
321
  attr_accessor :back_trace_limit
227
322
 
228
- # Alias for #use_readline
229
- alias use_readline? use_readline
323
+ # Alias for #use_multiline
324
+ alias use_multiline? use_multiline
325
+ # Alias for #use_singleline
326
+ alias use_singleline? use_singleline
327
+ # backward compatibility
328
+ alias use_reidline use_multiline
329
+ # backward compatibility
330
+ alias use_reidline? use_multiline
331
+ # backward compatibility
332
+ alias use_readline use_singleline
333
+ # backward compatibility
334
+ alias use_readline? use_singleline
335
+ # Alias for #use_colorize
336
+ alias use_colorize? use_colorize
337
+ # Alias for #use_autocomplete
338
+ alias use_autocomplete? use_autocomplete
230
339
  # Alias for #rc
231
340
  alias rc? rc
232
341
  alias ignore_sigint? ignore_sigint
233
342
  alias ignore_eof? ignore_eof
234
343
  alias echo? echo
344
+ alias echo_on_assignment? echo_on_assignment
345
+ alias newline_before_multiline_output? newline_before_multiline_output
235
346
 
236
347
  # Returns whether messages are displayed or not.
237
348
  def verbose?
238
349
  if @verbose.nil?
239
- if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
350
+ if @io.kind_of?(ReidlineInputMethod)
351
+ false
352
+ elsif defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
240
353
  false
241
354
  elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
242
355
  true
@@ -249,9 +362,11 @@ module IRB
249
362
  end
250
363
 
251
364
  # Whether #verbose? is +true+, and +input_method+ is either
252
- # StdioInputMethod or ReadlineInputMethod, see #io for more information.
365
+ # StdioInputMethod or ReidlineInputMethod or ReadlineInputMethod, see #io
366
+ # for more information.
253
367
  def prompting?
254
368
  verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
369
+ @io.kind_of?(ReidlineInputMethod) ||
255
370
  (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
256
371
  end
257
372
 
@@ -276,6 +391,7 @@ module IRB
276
391
  @prompt_c = pconf[:PROMPT_C]
277
392
  @prompt_n = pconf[:PROMPT_N]
278
393
  @return_format = pconf[:RETURN]
394
+ @return_format = "%s\n" if @return_format == nil
279
395
  if ai = pconf.include?(:AUTO_INDENT)
280
396
  @auto_indent_mode = ai
281
397
  else
@@ -350,36 +466,11 @@ module IRB
350
466
  @inspect_mode
351
467
  end
352
468
 
353
- # Obsolete method.
354
- #
355
- # Can be set using the +--noreadline+ and +--readline+ command line
356
- # options.
357
- #
358
- # See IRB@Command+line+options for more command line options.
359
- def use_readline=(opt)
360
- print "This method is obsolete."
361
- print "Do nothing."
362
- end
363
-
364
- # Sets the debug level of irb
365
- #
366
- # Can also be set using the +--irb_debug+ command line option.
367
- #
368
- # See IRB@Command+line+options for more command line options.
369
- def debug_level=(value)
370
- @debug_level = value
371
- RubyLex.debug_level = value
372
- end
373
-
374
- # Whether or not debug mode is enabled, see #debug_level=.
375
- def debug?
376
- @debug_level > 0
377
- end
378
-
379
469
  def evaluate(line, line_no, exception: nil) # :nodoc:
380
470
  @line_no = line_no
381
471
  if exception
382
- line = "begin ::Kernel.raise _; rescue _.class; #{line}; end"
472
+ line_no -= 1
473
+ line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
383
474
  @workspace.local_variable_set(:_, exception)
384
475
  end
385
476
  set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
@@ -393,6 +484,8 @@ module IRB
393
484
  # Exits the current session, see IRB.irb_exit
394
485
  def exit(ret = 0)
395
486
  IRB.irb_exit(@irb, ret)
487
+ rescue UncaughtThrowError
488
+ super
396
489
  end
397
490
 
398
491
  NOPRINTING_IVARS = ["@last_value"] # :nodoc:
@@ -0,0 +1,138 @@
1
+ require "reline"
2
+
3
+ module IRB
4
+ class << self
5
+ class Vec
6
+ def initialize(x, y, z)
7
+ @x, @y, @z = x, y, z
8
+ end
9
+
10
+ attr_reader :x, :y, :z
11
+
12
+ def sub(other)
13
+ Vec.new(@x - other.x, @y - other.y, @z - other.z)
14
+ end
15
+
16
+ def dot(other)
17
+ @x*other.x + @y*other.y + @z*other.z
18
+ end
19
+
20
+ def cross(other)
21
+ ox, oy, oz = other.x, other.y, other.z
22
+ Vec.new(@y*oz-@z*oy, @z*ox-@x*oz, @x*oy-@y*ox)
23
+ end
24
+
25
+ def normalize
26
+ r = Math.sqrt(self.dot(self))
27
+ Vec.new(@x / r, @y / r, @z / r)
28
+ end
29
+ end
30
+
31
+ class Canvas
32
+ def initialize((h, w))
33
+ @data = (0..h-2).map { [0] * w }
34
+ @scale = [w / 2.0, h-2].min
35
+ @center = Complex(w / 2, h-2)
36
+ end
37
+
38
+ def line((x1, y1), (x2, y2))
39
+ p1 = Complex(x1, y1) / 2 * @scale + @center
40
+ p2 = Complex(x2, y2) / 2 * @scale + @center
41
+ line0(p1, p2)
42
+ end
43
+
44
+ private def line0(p1, p2)
45
+ mid = (p1 + p2) / 2
46
+ if (p1 - p2).abs < 1
47
+ x, y = mid.rect
48
+ @data[y / 2][x] |= (y % 2 > 1 ? 2 : 1)
49
+ else
50
+ line0(p1, mid)
51
+ line0(p2, mid)
52
+ end
53
+ end
54
+
55
+ def draw
56
+ @data.each {|row| row.fill(0) }
57
+ yield
58
+ @data.map {|row| row.map {|n| " ',;"[n] }.join }.join("\n")
59
+ end
60
+ end
61
+
62
+ class RubyModel
63
+ def initialize
64
+ @faces = init_ruby_model
65
+ end
66
+
67
+ def init_ruby_model
68
+ cap_vertices = (0..5).map {|i| Vec.new(*Complex.polar(1, i * Math::PI / 3).rect, 1) }
69
+ middle_vertices = (0..5).map {|i| Vec.new(*Complex.polar(2, (i + 0.5) * Math::PI / 3).rect, 0) }
70
+ bottom_vertex = Vec.new(0, 0, -2)
71
+
72
+ faces = [cap_vertices]
73
+ 6.times do |j|
74
+ i = j-1
75
+ faces << [cap_vertices[i], middle_vertices[i], cap_vertices[j]]
76
+ faces << [cap_vertices[j], middle_vertices[i], middle_vertices[j]]
77
+ faces << [middle_vertices[i], bottom_vertex, middle_vertices[j]]
78
+ end
79
+
80
+ faces
81
+ end
82
+
83
+ def render_frame(i)
84
+ angle = i / 10.0
85
+ dir = Vec.new(*Complex.polar(1, angle).rect, Math.sin(angle)).normalize
86
+ dir2 = Vec.new(*Complex.polar(1, angle - Math::PI/2).rect, 0)
87
+ up = dir.cross(dir2)
88
+ nm = dir.cross(up)
89
+ @faces.each do |vertices|
90
+ v0, v1, v2, = vertices
91
+ if v1.sub(v0).cross(v2.sub(v0)).dot(dir) > 0
92
+ points = vertices.map {|p| [nm.dot(p), up.dot(p)] }
93
+ (points + [points[0]]).each_cons(2) do |p1, p2|
94
+ yield p1, p2
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ private def easter_egg(type = nil)
102
+ type ||= [:logo, :dancing].sample
103
+ case type
104
+ when :logo
105
+ File.open(File.join(__dir__, 'ruby_logo.aa')) do |f|
106
+ require "rdoc"
107
+ RDoc::RI::Driver.new.page do |io|
108
+ IO.copy_stream(f, io)
109
+ end
110
+ end
111
+ when :dancing
112
+ begin
113
+ canvas = Canvas.new(Reline.get_screen_size)
114
+ Reline::IOGate.set_winch_handler do
115
+ canvas = Canvas.new(Reline.get_screen_size)
116
+ end
117
+ ruby_model = RubyModel.new
118
+ print "\e[?1049h"
119
+ 0.step do |i| # TODO (0..).each needs Ruby 2.6 or later
120
+ buff = canvas.draw do
121
+ ruby_model.render_frame(i) do |p1, p2|
122
+ canvas.line(p1, p2)
123
+ end
124
+ end
125
+ buff[0, 20] = "\e[0mPress Ctrl+C to stop\e[31m\e[1m"
126
+ print "\e[H" + buff
127
+ sleep 0.05
128
+ end
129
+ rescue Interrupt
130
+ ensure
131
+ print "\e[0m\e[?1049l"
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ IRB.__send__(:easter_egg, ARGV[0]&.to_sym) if $0 == __FILE__
@@ -43,4 +43,3 @@ module IRB # :nodoc:
43
43
  end
44
44
  end
45
45
  end
46
-
@@ -22,7 +22,7 @@ module IRB # :nodoc:
22
22
  def set_last_value(value)
23
23
  _set_last_value(value)
24
24
 
25
- if @eval_history
25
+ if defined?(@eval_history) && @eval_history
26
26
  @eval_history_values.push @line_no, @last_value
27
27
  @workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
28
28
  end
@@ -30,9 +30,13 @@ module IRB # :nodoc:
30
30
  @last_value
31
31
  end
32
32
 
33
- # The command result history limit.
33
+ remove_method :eval_history= if method_defined?(:eval_history=)
34
+ # The command result history limit. This method is not available until
35
+ # #eval_history= was called with non-nil value (directly or via
36
+ # setting <code>IRB.conf[:EVAL_HISTORY]</code> in <code>.irbrc</code>).
34
37
  attr_reader :eval_history
35
- # Sets command result history limit.
38
+ # Sets command result history limit. Default value is set from
39
+ # <code>IRB.conf[:EVAL_HISTORY]</code>.
36
40
  #
37
41
  # +no+ is an Integer or +nil+.
38
42
  #
@@ -41,6 +45,9 @@ module IRB # :nodoc:
41
45
  # If +no+ is 0, the number of history items is unlimited.
42
46
  #
43
47
  # If +no+ is +nil+, execution result history isn't used (default).
48
+ #
49
+ # History values are available via <code>__</code> variable, see
50
+ # IRB::History.
44
51
  def eval_history=(no)
45
52
  if no
46
53
  if defined?(@eval_history) && @eval_history
@@ -58,20 +65,51 @@ module IRB # :nodoc:
58
65
  end
59
66
  end
60
67
 
61
- class History # :nodoc:
62
-
63
- def initialize(size = 16)
68
+ # Represents history of results of previously evaluated commands.
69
+ #
70
+ # Available via <code>__</code> variable, only if <code>IRB.conf[:EVAL_HISTORY]</code>
71
+ # or <code>IRB::CurrentContext().eval_history</code> is non-nil integer value
72
+ # (by default it is +nil+).
73
+ #
74
+ # Example (in `irb`):
75
+ #
76
+ # # Initialize history
77
+ # IRB::CurrentContext().eval_history = 10
78
+ # # => 10
79
+ #
80
+ # # Perform some commands...
81
+ # 1 + 2
82
+ # # => 3
83
+ # puts 'x'
84
+ # # x
85
+ # # => nil
86
+ # raise RuntimeError
87
+ # # ...error raised
88
+ #
89
+ # # Inspect history (format is "<item number> <evaluated value>":
90
+ # __
91
+ # # => 1 10
92
+ # # 2 3
93
+ # # 3 nil
94
+ #
95
+ # __[1]
96
+ # # => 10
97
+ #
98
+ class History
99
+
100
+ def initialize(size = 16) # :nodoc:
64
101
  @size = size
65
102
  @contents = []
66
103
  end
67
104
 
68
- def size(size)
105
+ def size(size) # :nodoc:
69
106
  if size != 0 && size < @size
70
107
  @contents = @contents[@size - size .. @size]
71
108
  end
72
109
  @size = size
73
110
  end
74
111
 
112
+ # Get one item of the content (both positive and negative indexes work).
75
113
  def [](idx)
76
114
  begin
77
115
  if idx >= 0
@@ -84,14 +122,14 @@ module IRB # :nodoc:
84
122
  end
85
123
  end
86
124
 
87
- def push(no, val)
125
+ def push(no, val) # :nodoc:
88
126
  @contents.push [no, val]
89
127
  @contents.shift if @size != 0 && @contents.size > @size
90
128
  end
91
129
 
92
130
  alias real_inspect inspect
93
131
 
94
- def inspect
132
+ def inspect # :nodoc:
95
133
  if @contents.empty?
96
134
  return real_inspect
97
135
  end
@@ -115,5 +153,3 @@ module IRB # :nodoc:
115
153
  end
116
154
  end
117
155
  end
118
-
119
-