Test if file is loaded

Why when I type load “shadows.rb” it always adds new menu into Plugins? I tried “shadows.rb”, “/shadows.rb”, “\shadows.rb” and now am lost

PLUGIN_ROOT = File.dirname(__FILE__)
if not (file_loaded? PLUGIN_ROOT << "\\shadows.rb" )
	UI.menu("Plugins").add_item("Barracuda Shadows") { barr_shadows; centerCamera(); }
end

Try to you explain to yourself step by step what you are doing and why. Then you would discover that you are doing something weird.

In Ruby, strings are mutable. The << operator is short for append, so the receiver string will be modified so it has the argument string at the end.

When repeatedly loading your file your are doing the following:

  1. You declare a constant (or redeclare it, causing a warning) PLUGIN_ROOT, which is a folder path like …\Plugins\bc_shadows.
  2. Then you append \shadows.rb, so PLUGIN_ROOT now looks like: …\Plugins\bc_shadows\shadows.rb
  3. Then you check whether this string is contained in the already registered file paths (which is false).

In the condition block, you have to register that this file path (or what ever string you use) has been loaded:

PLUGIN_ROOT = File.expand_path(File.dirname(__FILE__)) # normalize the path
shadows_file = File.join(PLUGIN_ROOT, "shadows.rb")

unless file_loaded(shadows_file)
  # UI stuff
  file_loaded(shadows_file)
end

You can also just use __FILE__ to refer to the file that the Ruby interpreter currently reads (file_loaded?(__FILE__)).

(By the way, if you use / as file path separator you don’t have to escape and it is internally preferred in Ruby.)

Thanks. I have corrected it a bit:

PLUGIN_ROOT = File.dirname(__FILE__)
file = PLUGIN_ROOT + "\shadows.rb"
if not (file_loaded? file )
	UI.menu("Plugins").add_item("Barracuda Shadows") { barr_shadows; centerCamera(); }
  file_loaded file
end  

Now it works better, but I get working that PLUGIN_ROOT constant was already defined.

Yes, you corrected it a bit…
Please don’t use file separators directly, but let Ruby do it because it knows better all edge cases:
file = File.join(PLUGIN_ROOT, "shadows.rb")

By the way, \s is a short hand for a space character. \ is an escape sign that is used to express control characters (line breaks etc.) with non-control characters. That’s why I suggested \ should not be used in file paths, otherwise you have to escape it as \\ (and in some situations even \\\\ or \\\\\\\\).
There must be a good reason why all the internet uses / as separator…

Warnings can be ignored (they don’t interrupt execution of the code). If you find it annoying you can avoid the situation by checking first if the constant has already been defined:

PLUGIN_ROOT = File.dirname(__FILE__) unless defined?(PLUGIN_ROOT)

That’s because PLUGIN_ROOT is assigned outside the part of the code protected by the if not (file_loaded? file) clause. The assignment is therefore executed again each time you load whatever file this snippet is from. Ruby will warn when you assign a value to a constant that already exists - even if it is the same value.

It isn’t clear to me why you are re-running this snippet. If you are doing it manually while testing out code, you could just ignore the warning. Or you could make that statement conditional:

PLUGIN_ROOT = File.dirname(__FILE__) unless defined? PLUGIN_ROOT
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.