@@ -11,7 +11,7 @@ module IRB
1111
1212 module Command
1313 class Ls < Base
14- include RubyArgsExtractor
14+ class EvaluationError < StandardError ; end
1515
1616 category "Context"
1717 description "Show methods, constants, and variables."
@@ -22,24 +22,35 @@ class Ls < Base
2222 -g [query] Filter the output with a query.
2323 HELP_MESSAGE
2424
25+ def evaluate ( code )
26+ @irb_context . workspace . binding . eval ( code )
27+ rescue Exception => e
28+ puts "#{ e . class } : #{ e . message } "
29+ raise EvaluationError
30+ end
31+
2532 def execute ( arg )
2633 if match = arg . match ( /\A (?<target>.+\s |)(-g|-G)\s +(?<grep>.+)$/ )
27- if match [ :target ] . empty?
28- use_main = true
29- else
30- obj = @irb_context . workspace . binding . eval ( match [ :target ] )
31- end
34+ target = match [ :target ]
3235 grep = Regexp . new ( match [ :grep ] )
36+ elsif match = arg . match ( /\A ((?<target>.+),|)\s *grep:(?<grep>.+)/ )
37+ # Legacy style `ls obj, grep: /regexp/`
38+ # Evaluation order should be eval(target) then eval(grep)
39+ target = match [ :target ] || ''
40+ grep_regexp_code = match [ :grep ]
3341 else
34- args , kwargs = ruby_args ( arg )
35- use_main = args . empty?
36- obj = args . first
37- grep = kwargs [ :grep ]
42+ target = arg . strip
3843 end
3944
40- if use_main
45+ if target . empty?
4146 obj = irb_context . workspace . main
4247 locals = irb_context . workspace . binding . local_variables
48+ else
49+ obj = evaluate ( target )
50+ end
51+
52+ if grep_regexp_code
53+ grep = evaluate ( grep_regexp_code )
4354 end
4455
4556 o = Output . new ( grep : grep )
@@ -52,6 +63,7 @@ def execute(arg)
5263 o . dump ( "class variables" , klass . class_variables )
5364 o . dump ( "locals" , locals ) if locals
5465 o . print_result
66+ rescue EvaluationError
5567 end
5668
5769 def dump_methods ( o , klass , obj )
0 commit comments