Today I Learned

hashrocket A Hashrocket project

Override Methods in a Pry Session

Today I learned you can override methods on an object from within pry! Super helpful if you want to test something out but not change it in code yet. It's important to note this only overrides at the instance level, not the class level, so other instances won't be affected.

class User
  def name = "tony"
end

pry(main)> me = User.new
pry(main)> me.name
# => "tony"
pry(main)> def me.name = "who now?"
pry(main)> me.name
# => "who now?"
pry(main)> other_me = User.new
pry(main)> other_me.name
# => "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.