If you change a file then re-load it in pry or irb, it seems to pick up any NEW functionality you've added to that class, but doesn't forget about OLD functionality you've deleted from that class.
Steps to reproduce:
- Create a class with a single method - eg.
say_hello
. - Open PRY or IRB, and
load 'my_class.rb'
- Edit your class - delete the existing method, and add a new one with a different name - eg.
say_goodbye
- reload your class -
load 'my_class.rb'
BOTH your methods will now be available. I see why this happens - because ruby allows you to re-open classes for modification, re-loading your file basically just re-opens the existing version of the class you'd already loaded, rather than wiping the memory of that class and defining the class again from scratch.
My question is, how do you work around this, apart from quitting and re-starting PRY or IRB? How do you just say "forget my previous class completely and re-load this file from scratch"?
Thanks!
You can use remove_const to remove the class from its parent, either from the
Module
it is in:or from
Object
if it was not declared inside a Module: