From 623ecdace6c9201aec8ae5dca88770cee6c85a37 Mon Sep 17 00:00:00 2001 From: k0kubun Date: Mon, 12 Nov 2018 00:55:34 +0000 Subject: Document binding.irb on Binding [ci skip] For some reason this very useful method was undocumented since it was added in 493e48897421d176a8faf0f0820323d79ecdf94a which makes finding it in the docs impossible before this change. I've added a detailed example with sample code because it's one of the most powerful tools to debug Ruby code and I believe very few people are aware of it due to the lack of documentation. [Fix GH-2010] From: Olivier Lacan git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65674 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/irb.rb | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'lib/irb.rb') diff --git a/lib/irb.rb b/lib/irb.rb index 85953346a7..eb9547fbb2 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -733,7 +733,63 @@ module IRB end class Binding - # :nodoc: + # Opens an IRB session where `binding.irb` is called which allows for + # interactive debugging. You can call any methods or variables available in + # the current scope, and mutate state if you need to. + # + # + # Given a Ruby file called `potato.rb` containing the following code: + # + # class Potato + # def initialize + # @cooked = false + # binding.irb + # puts @cooked + # end + # end + # + # Potato.new + # + # Running `ruby potato.rb` will open an IRB session where `binding.irb` is + # called, and you will see the following: + # + # $ ruby potato.rb + # + # From: potato.rb @ line 3 : + # + # 1: class Potato + # 2: def initialize + # 3: @cooked = false + # => 4: binding.irb + # 5: puts "Cooked potato: #{@cooked}" + # 6: end + # 7: end + # 8: + # 9: Potato.new + # + # irb(#):001:0> + # + # You can type any valid Ruby code and it will be evaluated in the current + # context. This allows you to debug without having to run your code repeatedly: + # + # irb(#):001:0> @cooked + # => false + # irb(#):002:0> self.class + # => Potato + # irb(#):003:0> caller.first + # => ".../2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval'" + # irb(#):004:0> @cooked = true + # => true + # + # You can exit the IRB session with the `exit` command. Note that exiting will + # resume execution where +binding.call+ had paused it, as you can see from the + # output printed to standard output in this example: + # + # irb(#):005:0> exit + # $ Cooked potato: true + # + # + # See IRB@IRB+Usage for more information. def irb IRB.setup(eval("__FILE__"), argv: []) workspace = IRB::WorkSpace.new(self) -- cgit v1.2.3