Skip to content

Commit 135f54a

Browse files
committed
support info consts ClassOrModule
1 parent 7cb4cfc commit 135f54a

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

lib/debug/session.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ def register_default_command
748748
# * `info ivars <expr>` shows the instance variables of the result of `<expr>`.
749749
# * `i[nfo] c or consts or constants`
750750
# * Show information about accessible constants except toplevel constants.
751+
# * `info consts <expr>` shows the constants of a class/module of the result of `<expr>`
751752
# * `i[nfo] g or globals or global_variables`
752753
# * Show information about global variables
753754
# * `i[nfo] th or threads`
@@ -800,7 +801,7 @@ def register_default_command
800801
when :ivars
801802
request_tc [:show, :ivars, pat, opt]
802803
when :consts
803-
request_tc [:show, :consts, pat]
804+
request_tc [:show, :consts, pat, opt]
804805
when :globals
805806
request_tc [:show, :globals, pat]
806807
when :threads

lib/debug/thread_client.rb

+37-19
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,6 @@ def show_locals pat
569569
end
570570

571571
def show_ivars pat, expr = nil
572-
573572
if expr && !expr.empty?
574573
_self = frame_eval(expr);
575574
elsif _self = current_frame&.self
@@ -585,18 +584,39 @@ def show_ivars pat, expr = nil
585584
end
586585
end
587586

588-
def show_consts pat, only_self: false
589-
if s = current_frame&.self
587+
def iter_consts c, names = {}
588+
c.constants(false).sort.each{|name|
589+
next if names.has_key? name
590+
names[name] = nil
591+
begin
592+
value = c.const_get(name)
593+
rescue Exception => e
594+
value = e
595+
end
596+
yield name, value
597+
}
598+
end
599+
600+
def get_consts expr = nil, only_self: false, &block
601+
if expr && !expr.empty?
602+
_self = frame_eval(expr)
603+
if M_KIND_OF_P.bind_call(_self, Module)
604+
iter_consts _self, &block
605+
return
606+
else
607+
raise "#{_self.inspect} (by #{expr}) is not a Module."
608+
end
609+
elsif _self = current_frame&.self
590610
cs = {}
591-
if M_KIND_OF_P.bind_call(s, Module)
592-
cs[s] = :self
611+
if M_KIND_OF_P.bind_call(_self, Module)
612+
cs[_self] = :self
593613
else
594-
s = M_CLASS.bind_call(s)
595-
cs[s] = :self unless only_self
614+
_self = M_CLASS.bind_call(_self)
615+
cs[_self] = :self unless only_self
596616
end
597617

598618
unless only_self
599-
s.ancestors.each{|c| break if c == Object; cs[c] = :ancestors}
619+
_self.ancestors.each{|c| break if c == Object; cs[c] = :ancestors}
600620
if b = current_frame&.binding
601621
b.eval('::Module.nesting').each{|c| cs[c] = :nesting unless cs.has_key? c}
602622
end
@@ -605,20 +625,17 @@ def show_consts pat, only_self: false
605625
names = {}
606626

607627
cs.each{|c, _|
608-
c.constants(false).sort.each{|name|
609-
next if names.has_key? name
610-
names[name] = nil
611-
begin
612-
value = c.const_get(name)
613-
rescue Exception => e
614-
value = e
615-
end
616-
puts_variable_info name, value, pat
617-
}
628+
iter_consts c, names, &block
618629
}
619630
end
620631
end
621632

633+
def show_consts pat, expr = nil, only_self: false
634+
get_consts expr, only_self: only_self do |name, value|
635+
puts_variable_info name, value, pat
636+
end
637+
end
638+
622639
SKIP_GLOBAL_LIST = %i[$= $KCODE $-K $SAFE].freeze
623640
def show_globals pat
624641
global_variables.sort.each{|name|
@@ -1105,7 +1122,8 @@ def wait_next_action_
11051122

11061123
when :consts
11071124
pat = args.shift
1108-
show_consts pat
1125+
expr = args.shift
1126+
show_consts pat, expr
11091127

11101128
when :globals
11111129
pat = args.shift

0 commit comments

Comments
 (0)