Search code examples
rubyread-eval-print-loopirb

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


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


Solution

  • 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