How can I add a new line in Ruby 2.7+ IRB multiline edit mode?

Viewed 407

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"?

2 Answers

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

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.

Related