Skip to content

Commit 4b012f5

Browse files
committed
support info ivars obj
`info ivars obj` shows the instance variables of `obj`. Note that this command notation also support `/pattern/` like `info ivars obj /pattern/` so that we can not show ivars of Regexp literal objects (but I believe nobody use it).
1 parent 9a9cc33 commit 4b012f5

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

lib/debug/session.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ def register_default_command
738738
# * It includes `self` as `%self` and a return value as `%return`.
739739
# * `i[nfo] i[var[s]]` or `i[nfo] instance`
740740
# * Show information about instance variables about `self`.
741+
# * `info ivars <expr>` shows the instance variables of the result of `<expr>`.
741742
# * `i[nfo] c[onst[s]]` or `i[nfo] constant[s]`
742743
# * Show information about accessible constants except toplevel constants.
743744
# * `i[nfo] g[lobal[s]]`
@@ -759,8 +760,9 @@ def register_default_command
759760
request_tc [:show, :default, pat] # something useful
760761
when 'l', /^locals?/
761762
request_tc [:show, :locals, pat]
762-
when 'i', /^ivars?/i, /^instance[_ ]variables?/i
763-
request_tc [:show, :ivars, pat]
763+
when /^i\b/, /^ivars?\b/i, /^instance[_ ]variables?\b/i
764+
expr = $~&.post_match&.strip
765+
request_tc [:show, :ivars, pat, expr]
764766
when 'c', /^consts?/i, /^constants?/i
765767
request_tc [:show, :consts, pat]
766768
when 'g', /^globals?/i, /^global[_ ]variables?/i

lib/debug/thread_client.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,18 @@ def show_locals pat
568568
end
569569
end
570570

571-
def show_ivars pat
572-
if s = current_frame&.self
573-
M_INSTANCE_VARIABLES.bind_call(s).sort.each{|iv|
574-
value = M_INSTANCE_VARIABLE_GET.bind_call(s, iv)
571+
def show_ivars pat, expr = nil
572+
573+
if expr && !expr.empty?
574+
_self = frame_eval(expr);
575+
elsif _self = current_frame&.self
576+
else
577+
_self = nil
578+
end
579+
580+
if _self
581+
M_INSTANCE_VARIABLES.bind_call(_self).sort.each{|iv|
582+
value = M_INSTANCE_VARIABLE_GET.bind_call(_self, iv)
575583
puts_variable_info iv, value, pat
576584
}
577585
end
@@ -1088,7 +1096,8 @@ def wait_next_action_
10881096

10891097
when :ivars
10901098
pat = args.shift
1091-
show_ivars pat
1099+
expr = args.shift
1100+
show_ivars pat, expr
10921101

10931102
when :consts
10941103
pat = args.shift

test/console/info_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,51 @@ def test_info_constant
204204
end
205205
end
206206
end
207+
208+
class InfoIvarsTest < ConsoleTestCase
209+
def program
210+
<<~RUBY
211+
1| class C
212+
2| def initialize
213+
3| @a = :a
214+
4| @b = :b
215+
5| end
216+
6| end
217+
7| c = C.new
218+
8| c
219+
RUBY
220+
end
221+
222+
def test_ivar
223+
debug_code program do
224+
type 'b 5'
225+
type 'c'
226+
assert_line_num 5
227+
type 'info ivars'
228+
assert_line_text(/@a/)
229+
assert_line_text(/@b/)
230+
type 'info ivars /b/'
231+
assert_no_line_text(/@a/)
232+
assert_line_text(/@b/)
233+
type 'c'
234+
end
235+
end
236+
237+
def test_ivar_obj
238+
debug_code program do
239+
type 'u 8'
240+
assert_line_num 8
241+
type 'info ivars'
242+
assert_no_line_text /@/
243+
type 'info ivars c'
244+
assert_line_text /@a/
245+
assert_line_text /@b/
246+
type 'info ivars c /b/'
247+
assert_no_line_text /@a/
248+
assert_line_text /@b/
249+
type 'c'
250+
end
251+
end
252+
253+
end
207254
end

0 commit comments

Comments
 (0)