How to copy multiple lines of code into byebug?

727 views Asked by At

byebug doesn't seem to be able to handle multiple lines of code.

Example

I put byebug into the controller, and the next lines of code are (these could be anything, just an example here):

    payment_intent = Stripe::PaymentIntent.create({
      payment_method_types: ['card'],
      amount: @amount_minor_unit,
      currency: @currency,
      application_fee_amount: 123, # STODO
      transfer_data: {
        destination: @star.stripe_account,
      },
    })

But it does this: enter image description here

If the code is edited so it's on one single line, it succeeds:

payment_intent = Stripe::PaymentIntent.create({payment_method_types: ['card'],amount: @amount_minor_unit,currency: @currency,application_fee_amount: 123, transfer_data: {destination: @star.stripe_account,},})

But this manual editing is extremely tedious. How can I make byebug accept multiple lines of code so that I can copy/paste directly from somewhere like a controller into the byebug-halted rails server?

Other ideas

After pressing ctrl+d in the console to exit byebug, then the browser errors, then I can copy code into the browser, but the same thing happens, where it accepts the code if it's all on one line:

enter image description here

..but otherwise errors with the same syntax error as above.

3

There are 3 answers

2
Clara On BEST ANSWER

I use the gem pry-byebug and it has the behaviour you want, you can paste multiple lines of code without it executing right away.

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

You can just add it to your gem file and set break points with binding.pry

2
adityapandit17 On

what works for me is Just copy all the code in one and paste it to the browser where we paste the link don't click enter. it will format it in one line. then just copy and paste in the byebug cli.

0
Cassandra S. On

The comments have already given you the answer, but I will formalize it here.

Instead of trying to copy and paste the line of code you want to execute, you can just use step to take a "step" through the code. Byebug's documentation gives you a list of commands you can use. Step will run your program one line at a time.

def index
  byebug
  init_item = {
    a: "foo", b: "bar"
  }
  // omitted code

Entering the breakpoint above, init_item will be nil. The command step (or s) will advance the code one line, and after that, init_item will be initialized to what we specify.

Sometimes the byebug will enter a few lines "down", but using up (for going up the callstack) will let you navigate to the line you're interested in.

There's no need to copy-paste code into byebug, you can just step through it.