2

I'm working with a local library (gem), let's call it B, inside my rails application A (Rails version 6.0.2.2)

In A's Gemfile, I require B via:

gem 'B', path: '../B'

When I do rails c with pry, I can access B and all its content correctly. However, when I make a local change to B and reopen rails c, the changes are not reflected.

If I quit the terminal (Mac OS) tab where I opened rails c and open another tab, suddenly all the changes that I made to B are reflected.

Does pry cache anything behind the scenes that's causing this flakiness?

platypus
  • 1,165
  • 5
  • 27
  • 47
  • 1
    You should check [how rails does class reloading and autoloading] (https://guides.rubyonrails.org/autoloading_and_reloading_constants_classic_mode.html). You can probably try `reload!` in pry to reload the environment which reloads environment. It is as good as exit console and start over. – Amit Patel May 03 '20 at 12:46

1 Answers1

0

Yes, your console is not reloaded automatically by Rails when you make changes.

This is to prevent triggering unexpected behavior in your console if you mix new objects with stale objects. However, you can reload your console manually by calling reload!.

Note, however, that all references to previous objects may be stale. E.g.:

User.object_id # => 70331650131980
user = User.first
# update the code...
reload!
User.object_id # => 70331615972020

As you can see by the object_id, the class is not the same after a reload. Therefore the user instance will still point to the first version of your code, before reloading. Be sure to fetch a fresh user in that case.

Sunny
  • 5,825
  • 2
  • 31
  • 41