4

Ruby 2.7 introduced an update to IRB that allows multiline editing. How can I add a new line into a multiline method to inject code between two previous statements?

E.g.

2.7.1 :019 > while session = server.accept
2.7.1 :020 >   session.puts "Hello World! The time is #{Time.now}"
2.7.1 :021 >   session.close
2.7.1 :022 > end

How do I add a new line before line 21's session.close so I can do something like session.puts "closing connection"?

Aaron
  • 13,349
  • 11
  • 66
  • 105

2 Answers2

6

On OS X hold option and press return on the line you'd like to put a new line after.

E.g.

2.7.1 :019 > while session = server.accept
2.7.1 :020 >   session.puts "Hello World! The time is #{Time.now}" # cursor here
2.7.1 :021 >   session.close
2.7.1 :022 > end

press option+return

Voilà

2.7.1 :019 > while session = server.accept
2.7.1 :020 >   session.puts "Hello World! The time is #{Time.now}"
2.7.1 :021 >   
2.7.1 :022 >   session.close
2.7.1 :023 > end
Aaron
  • 13,349
  • 11
  • 66
  • 105
  • 2
    My gut says the key combination for Linux / Windows will be similar with meta or alt + return. – Aaron Sep 15 '20 at 15:07
  • Gotcha. This was done on Mojave w/ bash. Is there a meta key used more than `option` in zsh? – Aaron Dec 05 '20 at 18:16
1

What did not work for me

My setup is with MacOS Monterey. The proposed solution ("press option+return") did not work. I tried a bunch of other key combinations, but they didn't work either.

What did work for me

I just deleted the end at the last line and that allowed me to use the "return" to insert new lines until I added the end again.