rubysl-irb 1.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -2
- data/lib/irb/cmd/chws.rb +6 -6
- data/lib/irb/cmd/fork.rb +10 -10
- data/lib/irb/cmd/help.rb +24 -14
- data/lib/irb/cmd/load.rb +8 -7
- data/lib/irb/cmd/nop.rb +8 -8
- data/lib/irb/cmd/pushws.rb +6 -5
- data/lib/irb/cmd/subirb.rb +6 -7
- data/lib/irb/completion.rb +90 -58
- data/lib/irb/context.rb +197 -30
- data/lib/irb/ext/change-ws.rb +17 -10
- data/lib/irb/ext/history.rb +20 -10
- data/lib/irb/ext/loader.rb +22 -12
- data/lib/irb/ext/math-mode.rb +16 -6
- data/lib/irb/ext/multi-irb.rb +69 -24
- data/lib/irb/ext/save-history.rb +87 -37
- data/lib/irb/ext/tracer.rb +17 -7
- data/lib/irb/ext/use-loader.rb +14 -6
- data/lib/irb/ext/workspaces.rb +16 -6
- data/lib/irb/extend-command.rb +92 -34
- data/lib/irb/frame.rb +18 -5
- data/lib/irb/help.rb +20 -19
- data/lib/irb/init.rb +156 -104
- data/lib/irb/input-method.rb +96 -23
- data/lib/irb/inspector.rb +145 -0
- data/lib/irb/lc/.document +4 -0
- data/lib/irb/lc/error.rb +8 -7
- data/lib/irb/lc/{help-message.rb → help-message} +14 -11
- data/lib/irb/lc/ja/encoding_aliases.rb +10 -0
- data/lib/irb/lc/ja/error.rb +19 -16
- data/lib/irb/lc/ja/help-message +33 -28
- data/lib/irb/locale.rb +83 -85
- data/lib/irb/magic-file.rb +37 -0
- data/lib/irb/notifier.rb +101 -15
- data/lib/irb/output-method.rb +38 -32
- data/lib/irb/ruby-lex.rb +143 -81
- data/lib/irb/ruby-token.rb +13 -19
- data/lib/irb/slex.rb +26 -27
- data/lib/irb/src_encoding.rb +4 -0
- data/lib/irb/version.rb +6 -7
- data/lib/irb/workspace.rb +22 -15
- data/lib/irb/ws-for-case-2.rb +5 -6
- data/lib/irb/xmp.rb +91 -4
- data/lib/rubysl/irb/irb.rb +523 -175
- data/lib/rubysl/irb/version.rb +1 -1
- data/rubysl-irb.gemspec +7 -6
- metadata +35 -15
data/lib/irb/ruby-token.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
#
|
2
|
-
# irb/ruby-token.rb - ruby tokens
|
3
|
-
# $Release Version: 0.9.
|
4
|
-
# $Revision
|
5
|
-
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
2
|
+
# irb/ruby-token.rb - ruby tokens
|
3
|
+
# $Release Version: 0.9.6$
|
4
|
+
# $Revision$
|
6
5
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
7
6
|
#
|
8
7
|
# --
|
9
8
|
#
|
10
|
-
#
|
11
9
|
#
|
10
|
+
#
|
11
|
+
# :stopdoc:
|
12
12
|
module RubyToken
|
13
13
|
EXPR_BEG = :EXPR_BEG
|
14
14
|
EXPR_MID = :EXPR_MID
|
@@ -18,20 +18,13 @@ module RubyToken
|
|
18
18
|
EXPR_DOT = :EXPR_DOT
|
19
19
|
EXPR_CLASS = :EXPR_CLASS
|
20
20
|
|
21
|
-
# for ruby 1.4X
|
22
|
-
if !defined?(Symbol)
|
23
|
-
Symbol = Integer
|
24
|
-
end
|
25
|
-
|
26
21
|
class Token
|
27
22
|
def initialize(seek, line_no, char_no)
|
28
23
|
@seek = seek
|
29
24
|
@line_no = line_no
|
30
25
|
@char_no = char_no
|
31
26
|
end
|
32
|
-
attr :seek
|
33
|
-
attr :line_no
|
34
|
-
attr :char_no
|
27
|
+
attr :seek, :line_no, :char_no
|
35
28
|
end
|
36
29
|
|
37
30
|
class TkNode < Token
|
@@ -58,7 +51,7 @@ module RubyToken
|
|
58
51
|
end
|
59
52
|
|
60
53
|
class TkOp < Token
|
61
|
-
|
54
|
+
attr_accessor :name
|
62
55
|
end
|
63
56
|
|
64
57
|
class TkOPASGN < TkOp
|
@@ -87,7 +80,7 @@ module RubyToken
|
|
87
80
|
if (tk = TkReading2Token[token]).nil?
|
88
81
|
IRB.fail TkReading2TokenNoKey, token
|
89
82
|
end
|
90
|
-
tk = Token(tk[0], value)
|
83
|
+
tk = Token(tk[0], value)
|
91
84
|
if tk.kind_of?(TkOp)
|
92
85
|
tk.name = token
|
93
86
|
end
|
@@ -96,8 +89,8 @@ module RubyToken
|
|
96
89
|
if (tk = TkSymbol2Token[token]).nil?
|
97
90
|
IRB.fail TkSymbol2TokenNoKey, token
|
98
91
|
end
|
99
|
-
return Token(tk[0], value)
|
100
|
-
else
|
92
|
+
return Token(tk[0], value)
|
93
|
+
else
|
101
94
|
if (token.ancestors & [TkId, TkVal, TkOPASGN, TkUnknownChar]).empty?
|
102
95
|
token.new(@prev_seek, @prev_line_no, @prev_char_no)
|
103
96
|
else
|
@@ -197,7 +190,7 @@ module RubyToken
|
|
197
190
|
[:TkASSOC, TkOp, "=>"],
|
198
191
|
[:TkQUESTION, TkOp, "?"], #?
|
199
192
|
[:TkCOLON, TkOp, ":"], #:
|
200
|
-
|
193
|
+
|
201
194
|
[:TkfLPAREN], # func( #
|
202
195
|
[:TkfLBRACK], # func[ #
|
203
196
|
[:TkfLBRACE], # func{ #
|
@@ -253,7 +246,7 @@ module RubyToken
|
|
253
246
|
IRB.fail AlreadyDefinedToken, token_n
|
254
247
|
end
|
255
248
|
token_c = eval("class #{token_n} < #{super_token}; end; #{token_n}")
|
256
|
-
|
249
|
+
|
257
250
|
if reading
|
258
251
|
if TkReading2Token[reading]
|
259
252
|
IRB.fail TkReading2TokenDuplicateError, token_n, reading
|
@@ -271,3 +264,4 @@ module RubyToken
|
|
271
264
|
def_token(*defs)
|
272
265
|
end
|
273
266
|
end
|
267
|
+
# :startdoc:
|
data/lib/irb/slex.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
#
|
2
|
-
# irb/slex.rb -
|
3
|
-
# $Release Version: 0.9.
|
4
|
-
# $Revision
|
5
|
-
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
2
|
+
# irb/slex.rb - simple lex analyzer
|
3
|
+
# $Release Version: 0.9.6$
|
4
|
+
# $Revision$
|
6
5
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
7
6
|
#
|
8
7
|
# --
|
9
8
|
#
|
10
|
-
#
|
9
|
+
#
|
11
10
|
#
|
12
11
|
|
13
12
|
require "e2mmap"
|
14
13
|
require "irb/notifier"
|
15
14
|
|
15
|
+
# :stopdoc:
|
16
16
|
module IRB
|
17
17
|
class SLex
|
18
|
-
@RCS_ID='-$Id
|
18
|
+
@RCS_ID='-$Id$-'
|
19
19
|
|
20
20
|
extend Exception2MessageMapper
|
21
21
|
def_exception :ErrNodeNothing, "node nothing"
|
@@ -25,20 +25,20 @@ module IRB
|
|
25
25
|
D_WARN = DOUT::def_notifier(1, "Warn: ")
|
26
26
|
D_DEBUG = DOUT::def_notifier(2, "Debug: ")
|
27
27
|
D_DETAIL = DOUT::def_notifier(4, "Detail: ")
|
28
|
-
|
28
|
+
|
29
29
|
DOUT.level = Notifier::D_NOMSG
|
30
30
|
|
31
31
|
def initialize
|
32
32
|
@head = Node.new("")
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def def_rule(token, preproc = nil, postproc = nil, &block)
|
36
36
|
D_DETAIL.pp token
|
37
37
|
|
38
38
|
postproc = block if block_given?
|
39
|
-
|
39
|
+
create(token, preproc, postproc)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def def_rules(*tokens, &block)
|
43
43
|
if block_given?
|
44
44
|
p = block
|
@@ -47,18 +47,18 @@ module IRB
|
|
47
47
|
def_rule(token, nil, p)
|
48
48
|
end
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
def preproc(token, proc)
|
52
52
|
node = search(token)
|
53
53
|
node.preproc=proc
|
54
54
|
end
|
55
|
-
|
56
|
-
#$BMW%A%'%C%/(B?
|
55
|
+
|
56
|
+
#$BMW%A%'%C%/(B?
|
57
57
|
def postproc(token)
|
58
58
|
node = search(token, proc)
|
59
59
|
node.postproc=proc
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
def search(token)
|
63
63
|
@head.search(token.split(//))
|
64
64
|
end
|
@@ -66,7 +66,7 @@ module IRB
|
|
66
66
|
def create(token, preproc = nil, postproc = nil)
|
67
67
|
@head.create_subnode(token.split(//), preproc, postproc)
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
def match(token)
|
71
71
|
case token
|
72
72
|
when Array
|
@@ -76,17 +76,17 @@ module IRB
|
|
76
76
|
return @head.match_io(token)
|
77
77
|
end
|
78
78
|
ret = @head.match(token)
|
79
|
-
D_DETAIL.exec_if{
|
79
|
+
D_DETAIL.exec_if{D_DETAIL.printf "match end: %s:%s\n", ret, token.inspect}
|
80
80
|
ret
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
def inspect
|
84
84
|
format("<SLex: @head = %s>", @head.inspect)
|
85
85
|
end
|
86
86
|
|
87
87
|
#----------------------------------------------------------------------
|
88
88
|
#
|
89
|
-
# class Node -
|
89
|
+
# class Node -
|
90
90
|
#
|
91
91
|
#----------------------------------------------------------------------
|
92
92
|
class Node
|
@@ -100,7 +100,7 @@ module IRB
|
|
100
100
|
|
101
101
|
attr_accessor :preproc
|
102
102
|
attr_accessor :postproc
|
103
|
-
|
103
|
+
|
104
104
|
def search(chrs, opt = nil)
|
105
105
|
return self if chrs.empty?
|
106
106
|
ch = chrs.shift
|
@@ -115,7 +115,7 @@ module IRB
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
def create_subnode(chrs, preproc = nil, postproc = nil)
|
120
120
|
if chrs.empty?
|
121
121
|
if @postproc
|
@@ -128,7 +128,7 @@ module IRB
|
|
128
128
|
end
|
129
129
|
return self
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
ch = chrs.shift
|
133
133
|
if node = @Tree[ch]
|
134
134
|
if chrs.empty?
|
@@ -162,7 +162,7 @@ module IRB
|
|
162
162
|
# chrs: String
|
163
163
|
# character array
|
164
164
|
# io must have getc()/ungetc(); and ungetc() must be
|
165
|
-
# able to be called arbitrary number of times.
|
165
|
+
# able to be called arbitrary number of times.
|
166
166
|
#
|
167
167
|
def match(chrs, op = "")
|
168
168
|
D_DETAIL.print "match>: ", chrs, "op:", op, "\n"
|
@@ -244,8 +244,7 @@ module IRB
|
|
244
244
|
end
|
245
245
|
end
|
246
246
|
end
|
247
|
-
|
248
|
-
SLex=IRB::SLex
|
247
|
+
# :startdoc:
|
249
248
|
|
250
249
|
if $0 == __FILE__
|
251
250
|
# Tracer.on
|
@@ -257,14 +256,14 @@ if $0 == __FILE__
|
|
257
256
|
print "1: ", tr.inspect, "\n"
|
258
257
|
tr.def_rule("==") {print "==\n"}
|
259
258
|
print "2: ", tr.inspect, "\n"
|
260
|
-
|
259
|
+
|
261
260
|
print "case 1:\n"
|
262
261
|
print tr.match("="), "\n"
|
263
262
|
print "case 2:\n"
|
264
263
|
print tr.match("=="), "\n"
|
265
264
|
print "case 3:\n"
|
266
265
|
print tr.match("=>"), "\n"
|
267
|
-
|
266
|
+
|
268
267
|
when "2"
|
269
268
|
tr = SLex.new
|
270
269
|
print "0: ", tr.inspect, "\n"
|
@@ -272,7 +271,7 @@ if $0 == __FILE__
|
|
272
271
|
print "1: ", tr.inspect, "\n"
|
273
272
|
tr.def_rule("==", proc{false}) {print "==\n"}
|
274
273
|
print "2: ", tr.inspect, "\n"
|
275
|
-
|
274
|
+
|
276
275
|
print "case 1:\n"
|
277
276
|
print tr.match("="), "\n"
|
278
277
|
print "case 2:\n"
|
data/lib/irb/version.rb
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
#
|
2
2
|
# irb/version.rb - irb version definition file
|
3
|
-
# $Release Version: 0.9.
|
4
|
-
# $Revision
|
5
|
-
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
3
|
+
# $Release Version: 0.9.6$
|
4
|
+
# $Revision$
|
6
5
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
7
6
|
#
|
8
7
|
# --
|
9
8
|
#
|
10
|
-
#
|
9
|
+
#
|
11
10
|
#
|
12
11
|
|
13
|
-
module IRB
|
14
|
-
@RELEASE_VERSION = "0.9.
|
15
|
-
@LAST_UPDATE_DATE = "
|
12
|
+
module IRB # :nodoc:
|
13
|
+
@RELEASE_VERSION = "0.9.6"
|
14
|
+
@LAST_UPDATE_DATE = "09/06/30"
|
16
15
|
end
|
data/lib/irb/workspace.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
#
|
2
|
-
# irb/workspace-binding.rb -
|
3
|
-
# $Release Version: 0.9.
|
4
|
-
# $Revision
|
5
|
-
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
2
|
+
# irb/workspace-binding.rb -
|
3
|
+
# $Release Version: 0.9.6$
|
4
|
+
# $Revision$
|
6
5
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
7
6
|
#
|
8
7
|
# --
|
9
8
|
#
|
10
|
-
#
|
11
9
|
#
|
12
|
-
|
10
|
+
#
|
11
|
+
module IRB # :nodoc:
|
13
12
|
class WorkSpace
|
14
|
-
#
|
13
|
+
# Creates a new workspace.
|
14
|
+
#
|
15
|
+
# set self to main if specified, otherwise
|
15
16
|
# inherit main from TOPLEVEL_BINDING.
|
16
17
|
def initialize(*main)
|
17
18
|
if main[0].kind_of?(Binding)
|
@@ -22,7 +23,7 @@ module IRB
|
|
22
23
|
case IRB.conf[:CONTEXT_MODE]
|
23
24
|
when 0 # binding in proc on TOPLEVEL_BINDING
|
24
25
|
@binding = eval("proc{binding}.call",
|
25
|
-
TOPLEVEL_BINDING,
|
26
|
+
TOPLEVEL_BINDING,
|
26
27
|
__FILE__,
|
27
28
|
__LINE__)
|
28
29
|
when 1 # binding in loaded file
|
@@ -38,8 +39,8 @@ EOF
|
|
38
39
|
when 2 # binding in loaded file(thread use)
|
39
40
|
unless defined? BINDING_QUEUE
|
40
41
|
require "thread"
|
41
|
-
|
42
|
-
IRB.const_set(
|
42
|
+
|
43
|
+
IRB.const_set(:BINDING_QUEUE, SizedQueue.new(1))
|
43
44
|
Thread.abort_on_exception = true
|
44
45
|
Thread.start do
|
45
46
|
eval "require \"irb/ws-for-case-2\"", TOPLEVEL_BINDING, __FILE__, __LINE__
|
@@ -49,8 +50,8 @@ EOF
|
|
49
50
|
@binding = BINDING_QUEUE.pop
|
50
51
|
|
51
52
|
when 3 # binging in function on TOPLEVEL_BINDING(default)
|
52
|
-
@binding = eval("def irb_binding; binding; end; irb_binding",
|
53
|
-
TOPLEVEL_BINDING,
|
53
|
+
@binding = eval("def irb_binding; private; binding; end; irb_binding",
|
54
|
+
TOPLEVEL_BINDING,
|
54
55
|
__FILE__,
|
55
56
|
__LINE__ - 3)
|
56
57
|
end
|
@@ -64,7 +65,7 @@ EOF
|
|
64
65
|
when Module
|
65
66
|
@binding = eval("IRB.conf[:__MAIN__].module_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
|
66
67
|
else
|
67
|
-
begin
|
68
|
+
begin
|
68
69
|
@binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
|
69
70
|
rescue TypeError
|
70
71
|
IRB.fail CantChangeBinding, @main.inspect
|
@@ -74,13 +75,17 @@ EOF
|
|
74
75
|
eval("_=nil", @binding)
|
75
76
|
end
|
76
77
|
|
78
|
+
# The Binding of this workspace
|
77
79
|
attr_reader :binding
|
80
|
+
# The top-level workspace of this context, also available as
|
81
|
+
# <code>IRB.conf[:__MAIN__]</code>
|
78
82
|
attr_reader :main
|
79
83
|
|
84
|
+
# Evaluate the given +statements+ within the context of this workspace.
|
80
85
|
def evaluate(context, statements, file = __FILE__, line = __LINE__)
|
81
86
|
eval(statements, @binding, file, line)
|
82
87
|
end
|
83
|
-
|
88
|
+
|
84
89
|
# error message manipulator
|
85
90
|
def filter_backtrace(bt)
|
86
91
|
case IRB.conf[:CONTEXT_MODE]
|
@@ -94,9 +99,11 @@ EOF
|
|
94
99
|
end
|
95
100
|
when 2
|
96
101
|
return nil if bt =~ /irb\/.*\.rb/
|
102
|
+
return nil if bt =~ /irb\.rb/
|
97
103
|
when 3
|
98
104
|
return nil if bt =~ /irb\/.*\.rb/
|
99
|
-
bt
|
105
|
+
return nil if bt =~ /irb\.rb/
|
106
|
+
bt = bt.sub(/:\s*in `irb_binding'/, '')
|
100
107
|
end
|
101
108
|
bt
|
102
109
|
end
|
data/lib/irb/ws-for-case-2.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
#
|
2
|
-
# irb/ws-for-case-2.rb -
|
3
|
-
# $Release Version: 0.9.
|
4
|
-
# $Revision
|
5
|
-
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
2
|
+
# irb/ws-for-case-2.rb -
|
3
|
+
# $Release Version: 0.9.6$
|
4
|
+
# $Revision$
|
6
5
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
7
6
|
#
|
8
7
|
# --
|
9
8
|
#
|
10
|
-
#
|
9
|
+
#
|
11
10
|
#
|
12
11
|
|
13
12
|
while true
|
14
|
-
IRB::BINDING_QUEUE.push
|
13
|
+
IRB::BINDING_QUEUE.push _ = binding
|
15
14
|
end
|
data/lib/irb/xmp.rb
CHANGED
@@ -1,21 +1,57 @@
|
|
1
1
|
#
|
2
2
|
# xmp.rb - irb version of gotoken xmp
|
3
3
|
# $Release Version: 0.9$
|
4
|
-
# $Revision
|
5
|
-
# $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
|
4
|
+
# $Revision$
|
6
5
|
# by Keiju ISHITSUKA(Nippon Rational Inc.)
|
7
6
|
#
|
8
7
|
# --
|
9
8
|
#
|
10
|
-
#
|
9
|
+
#
|
11
10
|
#
|
12
11
|
|
13
12
|
require "irb"
|
14
13
|
require "irb/frame"
|
15
14
|
|
15
|
+
# An example printer for irb.
|
16
|
+
#
|
17
|
+
# It's much like the standard library PrettyPrint, that shows the value of each
|
18
|
+
# expression as it runs.
|
19
|
+
#
|
20
|
+
# In order to use this library, you must first require it:
|
21
|
+
#
|
22
|
+
# require 'irb/xmp'
|
23
|
+
#
|
24
|
+
# Now, you can take advantage of the Object#xmp convenience method.
|
25
|
+
#
|
26
|
+
# xmp <<END
|
27
|
+
# foo = "bar"
|
28
|
+
# baz = 42
|
29
|
+
# END
|
30
|
+
# #=> foo = "bar"
|
31
|
+
# #==>"bar"
|
32
|
+
# #=> baz = 42
|
33
|
+
# #==>42
|
34
|
+
#
|
35
|
+
# You can also create an XMP object, with an optional binding to print
|
36
|
+
# expressions in the given binding:
|
37
|
+
#
|
38
|
+
# ctx = binding
|
39
|
+
# x = XMP.new ctx
|
40
|
+
# x.puts
|
41
|
+
# #=> today = "a good day"
|
42
|
+
# #==>"a good day"
|
43
|
+
# ctx.eval 'today # is what?'
|
44
|
+
# #=> "a good day"
|
16
45
|
class XMP
|
17
|
-
@RCS_ID='-$Id
|
46
|
+
@RCS_ID='-$Id$-'
|
18
47
|
|
48
|
+
# Creates a new XMP object.
|
49
|
+
#
|
50
|
+
# The top-level binding or, optional +bind+ parameter will be used when
|
51
|
+
# creating the workspace. See WorkSpace.new for more information.
|
52
|
+
#
|
53
|
+
# This uses the +:XMP+ prompt mode, see IRB@Customizing+the+IRB+Prompt for
|
54
|
+
# full detail.
|
19
55
|
def initialize(bind = nil)
|
20
56
|
IRB.init_config(nil)
|
21
57
|
#IRB.parse_opts
|
@@ -33,6 +69,17 @@ class XMP
|
|
33
69
|
IRB.conf[:MAIN_CONTEXT] = @irb.context
|
34
70
|
end
|
35
71
|
|
72
|
+
# Evaluates the given +exps+, for example:
|
73
|
+
#
|
74
|
+
# require 'irb/xmp'
|
75
|
+
# x = XMP.new
|
76
|
+
#
|
77
|
+
# x.puts '{:a => 1, :b => 2, :c => 3}'
|
78
|
+
# #=> {:a => 1, :b => 2, :c => 3}
|
79
|
+
# # ==>{:a=>1, :b=>2, :c=>3}
|
80
|
+
# x.puts 'foo = "bar"'
|
81
|
+
# # => foo = "bar"
|
82
|
+
# # ==>"bar"
|
36
83
|
def puts(exps)
|
37
84
|
@io.puts exps
|
38
85
|
|
@@ -52,16 +99,22 @@ class XMP
|
|
52
99
|
end
|
53
100
|
end
|
54
101
|
|
102
|
+
# A custom InputMethod class used by XMP for evaluating string io.
|
55
103
|
class StringInputMethod < IRB::InputMethod
|
104
|
+
# Creates a new StringInputMethod object
|
56
105
|
def initialize
|
57
106
|
super
|
58
107
|
@exps = []
|
59
108
|
end
|
60
109
|
|
110
|
+
# Whether there are any expressions left in this printer.
|
61
111
|
def eof?
|
62
112
|
@exps.empty?
|
63
113
|
end
|
64
114
|
|
115
|
+
# Reads the next expression from this printer.
|
116
|
+
#
|
117
|
+
# See IO#gets for more information.
|
65
118
|
def gets
|
66
119
|
while l = @exps.shift
|
67
120
|
next if /^\s+$/ =~ l
|
@@ -72,12 +125,46 @@ class XMP
|
|
72
125
|
l
|
73
126
|
end
|
74
127
|
|
128
|
+
# Concatenates all expressions in this printer, separated by newlines.
|
129
|
+
#
|
130
|
+
# An Encoding::CompatibilityError is raised of the given +exps+'s encoding
|
131
|
+
# doesn't match the previous expression evaluated.
|
75
132
|
def puts(exps)
|
133
|
+
if @encoding and exps.encoding != @encoding
|
134
|
+
enc = Encoding.compatible?(@exps.join("\n"), exps)
|
135
|
+
if enc.nil?
|
136
|
+
raise Encoding::CompatibilityError, "Encoding in which the passed expression is encoded is not compatible to the preceding's one"
|
137
|
+
else
|
138
|
+
@encoding = enc
|
139
|
+
end
|
140
|
+
else
|
141
|
+
@encoding = exps.encoding
|
142
|
+
end
|
76
143
|
@exps.concat exps.split(/\n/)
|
77
144
|
end
|
145
|
+
|
146
|
+
# Returns the encoding of last expression printed by #puts.
|
147
|
+
attr_reader :encoding
|
78
148
|
end
|
79
149
|
end
|
80
150
|
|
151
|
+
# A convenience method that's only available when the you require the IRB::XMP standard library.
|
152
|
+
#
|
153
|
+
# Creates a new XMP object, using the given expressions as the +exps+
|
154
|
+
# parameter, and optional binding as +bind+ or uses the top-level binding. Then
|
155
|
+
# evaluates the given expressions using the +:XMP+ prompt mode.
|
156
|
+
#
|
157
|
+
# For example:
|
158
|
+
#
|
159
|
+
# require 'irb/xmp'
|
160
|
+
# ctx = binding
|
161
|
+
# xmp 'foo = "bar"', ctx
|
162
|
+
# #=> foo = "bar"
|
163
|
+
# #==>"bar"
|
164
|
+
# ctx.eval 'foo'
|
165
|
+
# #=> "bar"
|
166
|
+
#
|
167
|
+
# See XMP.new for more information.
|
81
168
|
def xmp(exps, bind = nil)
|
82
169
|
bind = IRB::Frame.top(1) unless bind
|
83
170
|
xmp = XMP.new(bind)
|