marcamillion
marcamillion

Reputation: 33755

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.

Upvotes: 20

Views: 6797

Answers (6)

ogirginc
ogirginc

Reputation: 5270

Starting with the Ruby version 2.7.0-preview1, it is possible to edit in IRB sessions if you press the button:

irb multiline edit

Source: https://www.ruby-lang.org/en/news/2019/05/30/ruby-2-7-0-preview1-released/

Upvotes: 3

Andrew Marshall
Andrew Marshall

Reputation: 96914

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).

Upvotes: 17

horseyguy
horseyguy

Reputation: 29895

You can do this easily in Pry (a much more powerful alternative to IRB), simply use the edit-method command to reopen the method in an editor as follows:

[19] (pry) main: 0> def full_name(first, last)
[19] (pry) main: 0*   puts "Your full name is: #{first + '' + last}"  
[19] (pry) main: 0* end  
=> nil
[20] (pry) main: 0> edit full_name
Waiting for Emacs...
=> nil
[21] (pry) main: 0> show-method full_name

From: (pry) @ line 32:
Number of lines: 3
Owner: Object
Visibility: public

def full_name(first, middle, last)
  puts "Your full name is: #{first + middle + last}"
end
[22] (pry) main: 0> full_name "Stephen ", "william ", "Hawking"
Your full name is: Stephen william Hawking
=> nil
[23] (pry) main: 0> 

Pry automatically reloads the method after editing is complete (the editor pry uses can also be configured)

Upvotes: 14

shortdiv
shortdiv

Reputation: 165

You can edit this via irb or pry (which I would strongly recommend) - but you'll need to retype the whole thing. If you think about it one method isn't that difficult to type out :P However, if you have more than one method enclosed in a class, it can be very tedious to retype the entire class and corresponding methods. What you can do in that case is use class_evals.

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

# NEW Edited Code

Name.class_eval do 
  def full_name(first, middle, last)
    puts "Your full name is: #{first + ' ' + middle + ' ' + last}"
  end 
end

Here's a good resource to read about re-opening classes/monkeypatching and here's another that warns about the bad effects of monkeypatching

Upvotes: 1

Aleksander Pohl
Aleksander Pohl

Reputation: 1695

Check out the pry gem - a great replacement for IRB. This features might be helpful:

  • hist - replaying history of commands
  • amend-line - changing a line in a mulit-line entry

They are well documented on the pry wiki

Upvotes: 3

Niklas B.
Niklas B.

Reputation: 95278

I don't think you have a lot of options here. What I usually do is to place the code I'm playing with in a file and use load '/path/to/file.rb' to reload it whenever I change something.

You can also try the interactive_editor gem which allows you to use a full-blown text editor for text editing inside an IRB session.

Upvotes: 6

Related Questions