sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Menu Guards Using "defined?"

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 2 Posters 208 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      Jim
      last edited by

      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.

      Hi

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        That's interesting. But then you'd have an if statement for every menu item you add..?

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by

          @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.

          Hi

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @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.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              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.

              Hi

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement