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

2

6

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

Exhale answered 15/9, 2020 at 0:1 Comment(0)
E
8

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
Exhale answered 15/9, 2020 at 0:1 Comment(2)
My gut says the key combination for Linux / Windows will be similar with meta or alt + return.Exhale
Gotcha. This was done on Mojave w/ bash. Is there a meta key used more than option in zsh?Exhale
M
2

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.

Mercurialism answered 5/9, 2022 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.