Search code examples
rubyirb

If I defined a Ruby method in IRB, how do I edit that method without retyping everything?


Say I am running IRB and I type this into the console:

def full_name(first, last)
   puts "Your full name is: #{first, ' ', last}"
end

Say, that I wanted to edit that to include the parameter middle, how would I just bring back up that same method and edit the parameter list and edit the puts statement without having to retype the entire method?

P.S. I know that this example is simple and I could easily just retype the method, but I have much larger methods I am experimenting with and I am using this simple one for brevity.

Thanks.


Solution

  • You can't. Other than retyping/repasting it, or pressing to get all the previous statements, but for longer methods this can be very confusing.

    Why not type your code in an editor, and then do load 'mycode.rb' in IRb? This is essentially equivalent to copy-and-pasting the text in, and calling load 'myfile.rb' again will, as usual, override the existing method definitions.

    Or, even better, use Pry instead of IRB as suggested by bannister below (I completely replaced IRB with Pry long ago myself).