Menu Guards Using "defined?"
-
So I had this idea and was curious if anyone can see any problem with it?
Instead of this:
require 'sketchup' unless file_loaded?('file_name.rb') UI.menu.add_item('Lengthify') { lengthify() } file_loaded('file_name.rb') end
Do this:
unless defined?(lengthify) UI.menu.add_item('Lengthify') { lengthify() } end
I think it is more exact. I don't care if the file has already been loaded - I often need to load and reload a file many times to debug it. I do care if the method has already been defined.
I think you NEED to put this at the top of the file because the method would be defined if it were evaluated at the end of the file.
Here is some info on the defined? operator.
-
That's interesting. But then you'd have an if statement for every menu item you add..?
-
@thomthom said:
That's interesting. But then you'd have an if statement for every menu item you add..?
Good point. You could just assume since the one method is defined, all the methods in the file also got defined.
-
@jim said:
@thomthom said:
That's interesting. But then you'd have an if statement for every menu item you add..?
Good point. You could just assume since the one method is defined, all the methods in the file also got defined.
Then you're back to where you started.
Maybe a wrapper method to add menu items that check for the existence of the method it calls. Then when you add more menu items while you write your plugins you can just reload.
-
I have decided this is not an issue. It is only relevant for people who are re-loading a plugin for testing. A typical plugin does not even need menu guards at since it only gets required once anyway.
Advertisement