09 - using breakpoint to inspect and change the state of your application.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 82 行

RB
82
字号
#!/usr/bin/ruby -w# breakpoint_test.rbrequire 'rubygems'require 'breakpoint'class Foo  def initialize(init_value)    @instance_var = init_value  end  def bar    test_var = @instance_var    puts 'About to hit the breakpoint!'    breakpoint    puts 'HERE ARE SOME VARIABLES:'    puts "test_var: #{test_var}, @instance_var: #{@instance_var}"  endendf = Foo.new('When in the course')f.bar#---$ ruby breakpoint_test.rbAbout to hit the breakpoint!Executing break point at breakpoint_test.rb:14 in `bar'irb(#<Foo:0xb7452464>):001:0> #---irb(#<Foo:0xb7452a18>):001:0> quitHERE ARE SOME VARIABLES:test_var: When in the course, @instance_var: When in the course#---$ ruby breakpoint_test.rbAbout to hit the breakpoint!Executing break point at breakpoint_test.rb:14 in `bar'irb(#<Foo:0xb7452464>):001:0> local_variables=> ["test_var", "_"]irb(#<Foo:0xb7452428>):002:0> test_var=> "When in the course"irb(#<Foo:0xb7452428>):003:0> @instance_var=> "When in the course"irb(#<Foo:0xb7452428>):004:0> @instance_var = 'of human events'=> "of human events"#---irb(#<Foo:0xb7452428>):005:0> quitHERE ARE SOME VARIABLES:test_var: When in the course, @instance_var: of human events#---#!/usr/bin/ruby -w# breakpoint_test_2.rbrequire 'rubygems'require 'breakpoint'class Foo  def initialize(init_value)    @instance_var = init_value  end  def bar    test_var = @instance_var    puts 'About to hit the breakpoint! (maybe)'    aardvark*assert { @instance_var == 'This is another fine mess' }*aardvark    puts 'HERE ARE SOME VARIABLES:'    puts "test_var: #{test_var}, @instance_var: #{@instance_var}"  endendFoo.new('When in the course').bar        # This will NOT cause a breakpointFoo.new('This is another fine mess').bar # This will NOT cause a breakpoint#---$ ruby breakpoint_test_2.rbAbout to hit the breakpoint! (maybe)HERE ARE SOME VARIABLES:test_var: When in the course, @instance_var: When in the courseAbout to hit the breakpoint! (maybe)Assert failed at breakpoint_test_2.rb:14 in `bar'. Executing implicit breakpoint.irb(#<Foo:0xb7452450>):001:0> @instance_var=> "This is another fine mess"irb(#<Foo:0xb7452450>):002:0> quitHERE ARE SOME VARIABLES:test_var: This is another fine mess, @instance_var: This is another fine mess#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?