Cannot Re-load a Ruby Script using Require Command

#1

Hello all,
An extension of mine uses a Require command to search out a file on the user’s computer and load/run it in the model. The require command successfully loads/runs the file the first time, but when I try to use require to load/run the same file again it does not work. I tested this by running the Require command twice in a row and returning the status of each of the loads. The first one always comes back as “True” and the second status check always comes back as “False”.

On a previous forum post I found THIS LINK which mentions the following

The absolute path of the loaded file is added to $LOADED_FEATURES ( $" ). A file will not be loaded again if its path already appears in $" . For example, require 'a'; require './a' will not load a.rb again.

Does this mean that I somehow need to clear out the Loaded_Features? If this is correct, is there some ruby code that will allow me to do that without erasing my model?

Basically, I’m looking for a way to be able to use the Require command on the same file path more than once in the same SketchUp session.

Any help would be greatly appreciated.

Thanks

#2

Try load. You may have warnings about constants, etc being re-defined…

#3

require is a ‘load once’ method, load is the command for re-loading files, but primarily only if they have been modified since the last load…

if it’s a static file, you just need to hold a reference in your module space and call that each time…

john

#4

As said require is for loading something once, which is how you typically want to load code. The only reason to reload something if it has changed, e.g. when testing while coding. Don’t think of require as loading and running; running code should be done by calling the methods that encapsulate the code, not loading the containing file.

#5

Okay, so now I understand that the Require Tab can only be used to load something in once. That makes sense to me. With that being said, my extension allows the user to choose a texture from some dropdowns which then creates a model with the texture they selected. They can then redo what they just did but choose a new texture. The issue is that once they create a model with one texture and then create another model with a different texture, they can no longer choose the first texture again.

When a texture is chosen, the code jumps into a ruby script that is housed in one of the Plugin’s folders to set some variables and then jumps back out. Since I have been using the Require command I understand that once I jump into one of those folders, I can’t jump back in. Is there a way that I can jump into the same folder more than once? Thank you

#6

Wrap the code in a method and call that method when you want the code to run, rather than loading the file.