Skip to content Skip to sidebar Skip to footer

How to Exit Byebug Without Quitting Server Again

itten past a problems? => Say "farewell bye!" with a SUPER 'pry'!

Imagine if you could:

  1. Add as many binding.pry's you want to anywhere in your code while in a pry session without exiting and restarting!
  2. Go through your code from offset to finish and exist able to follow exactly the path/flow of all your methods?

Given this sample code:

          one:  def test_method
ii: ten.times.each do |i|
3: puts i
4: terminate

3. Be able to give a conditional pry such equally simply 'pry' if i == 5.

This & more is possible with…….

"Look!

Upwardly in the sky!

It'southward a bird!

Information technology'due south a plane!

Information technology'due south Super PRY!"

Let your pry proceeds superpowers with the "pry bye-bug" gem!

Upon starting my journey as a Flat Iron School Bootcamp student, I realized I would have to become best friends with what I thought was the equivalent of a dentist visit, crave 'pry'. During my pre-piece of work leading up to the program, I was introduced to this magical being 'pry' but it seemed then foreign to me and using it seemed like such a drag…like a long, punch upwards modem kind of lag. I grew up about of my life in NY then I am used to everything being quick, Type A, you get the betoken. And so, when I started using 'pry' at this time information technology felt like a momentum stopper to my coding.

Having to constantly cease coding and repeatedly put 'pry's' everywhere to see if my code worked and then having to go out out and rerun my programme seemed so tedious. I said to myself, "There has to be a ameliorate way to pry." I did some enquiry and sure enough, somebody made a special pry gem chosen "pry farewell-issues" that substantially supercharges pry. Let'south install it kickoff.

To install the gem:

Add:

          gem 'pry-byebug'        

To your Gemfile and run:

          'bundle install' or just 'bundle' (shortcut)        

          source "https://rubygems.org"          gem "sinatra-activerecord"
gem "sqlite3"
gem "pry"
gem "require_all"
jewel "dotenv"
gem "http"
gem 'pry-byebug'

Besides yous will need to use the original 'pry' method and then you demand to go on the crave 'pry'.

To activate pry-byebug's superpowers you lot can utilize pry as you normally would and it will execute on the first statement later your

          bounden.pry                  

After playing around with the jewel, I plant it near powerful to put the binding.pry in my "run file" right earlier the first method/statement. The reason is that this gem allows us to go through our code literally line by line or method past method. So past placing the binding.pry at the very offset nosotros can go through our whole code literally from start to cease. We'll use 1 of our API labs as a good case of pry-byebug in activeness.

          require_relative "../lib/api_communicator.rb"
require_relative "../lib/command_line_interface.rb"
require 'pry'
binding.pry
welcome
graphic symbol = get_character_from_user
show_character_movies(character)

Hither's some of my code so you lot tin have some context:

          def welcome
puts '*' * thirty
puts "Welcome to Star Wars SEARCH"
puts '*' * 30
end
def get_character_from_user
puts "please enter a character name"
user_input = "Luke Skywalker"
user_input
test_method
# apply gets to capture the user's input. This method should return that input, downcased.
cease
def test_method
10.times.each do |i|
puts i
end
terminate

Usually in this last motion picture the user_input would exist a gets.chomp only to come across my plan in action I difficult coded a possible search term, "Luke Skywalker" into it so that I can see how my program runs. **Also if you notice I put in the test_method method to be able to exam out the conditional pry functionality I mentioned earlier.

Now open the concluding and lets run

          ruby ./bin/run.rb        

Great! It stopped on the outset argument/method afterward the 'pry'. Here is a simple overview of our control options at this bespeak from the gem Readme file : (**My comments are followed by the hash sign**)

  1. intermission: Manage breakpoints. #This is essentially binding.pry's. More than on this later.
  2. step: Pace execution into the next line or method. Takes an optional numeric argument to pace multiple times. #This is for if yous want to get into the nitty gritty and when executed at a method will become into the method line by line every time you type "footstep".
  3. next: Step over to the side by side line within the same frame. Also takes an optional numeric argument to footstep multiple lines. #Will move onto the next method.
  4. cease: Execute until current stack frame returns.# Finishes the current method/stack.
  5. proceed: Continue programme execution and end the Pry session.# Runs the rest of the lawmaking same as if you were to type 'get out' in a normal 'pry'.

Step #ane currently is invalid as we exercise not have whatsoever breakpoints still.

If yous select #two "step" want to literally follow the menstruation of your code from kickoff to stop yous would but keep pressing 'step'. Or after doing it once, press the up arrow so enter in a loop. :)

If you select #3 "next" this just moves on to the next method.

Nosotros volition select #three "side by side"

Notice how the welcome method ran and we take now moved on to the next method. Lets dive into this one past choosing "step".

Now type "step" again to move to the side by side argument

What's really cool is that you can be in a pry where pry'south normally would not terminate such as line 11. But we can "pace" one more than time and we can test equally if we stopped there on a pry!

And so allow'southward have some fun and effort out a conditional pry also as setting upwardly some other pry at the test_method method on line 18 and xix.

First to set the equivalent of a binding.pry elsewhere we volition be using the "break" function.

Here is the official ReadMe from the gem mail service:

############################################
"You lot can gear up and adjust breakpoints directly from a Pry session using the pause command:

break: Set a new breakpoint from a line number in the current file, a file and line number, or a method. Laissez passer an optional expression to create a conditional breakpoint. Edit existing breakpoints via diverse flags.

Examples:

          break SomeClass#run            # Break at the commencement of `SomeClass#run`.
suspension Foo#bar if baz? # Break at `Foo#bar` simply if `baz?`.
pause app/models/user.rb:15 # Break at line 15 in user.rb.
break 14 # Break at line 14 in the current file.

break --condition 4 x > 2 # Change condition on breakpoint #iv to '10 > 2'.
break --status 3 # Remove the condition on breakpoint #3.

break --delete 5 # Delete breakpoint #5.
break --disable-all # Disable all breakpoints.

interruption # List all breakpoints.
break --show 2 # Show details about breakpoint #2.

Type break --help from a Pry session to meet all available options."

############################################

I volition be using the break function by "break eighteen" #This means make a breakpoint or a bounden.pry between line 17 and xviii. We should go a ostend message.

Lets try it.

Information technology should tell you that at that place is now a Breakpoint 1: /Users/…

At present lets as well make a conditional pry at line 19 past typing

          interruption line 19 if i == 5        

Nosotros should get a Breakpoint 2: bulletin confirming the cosmos.

At present lets type "continue" which as a reminder is,

"go on: Continue program execution and end the Pry session.# Runs the residue of the code same as if you were to type 'exit' in a normal 'pry'."

Yay! We hit our pry on 18. Permit's try to continue to see if information technology stops on 19 by "go on" again.

Look! Information technology stopped! And if nosotros check to run into what "i" is, it is 5! Great! So every bit you lot can meet the pry-byebug gem is really powerful and would be a great sidekick to any developer.

Please experience gratis to play around with this peachy tool on your own. I have added code snippets after each picture so you can endeavour out the pry-byebug for yourself.

Here is the official URL for the gem post:

https://github.com/deivid-rodriguez/pry-byebug

Thanks for stopping by!

Go out!

smithpleake1947.blogspot.com

Source: https://itnext.io/bitten-by-a-bug-say-bye-bye-with-a-super-pry-765eb3d7ea4

Post a Comment for "How to Exit Byebug Without Quitting Server Again"