Today I Learned

hashrocket A Hashrocket project

Edit Source Code from Within Pry

Yesterday I learned (YIL?!) how to override instance methods within pry. But what if you like the change and want make it more permanent? Typically what I would do is edit the appropriate file in my editor, and either reload! within pry or restart the session.

I learned you can also edit the code directly from within pry - if I want to edit the User class, I can run edit User and pry will open the file that defines User in my $EDITOR ((n)vim, natch). When you close the file, you'll be dropped back into your pry session and the code will be reloaded (including any instances of the class you already initialized in your session).

class User
  def name = "tony"
end

pry(main)> me = User.new
pry(main)> me.name
# => "tony"

pry(main)> edit User # and let's say we edit name to return 'not-tony'
pry(main)> me.name
# => "not-tony"
pry(main)> User.new.name
# => "not-tony"
See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.