sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Structure of my Ruby files

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 3 Posters 172 Views 3 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.
    • K Offline
      ksor
      last edited by

      I'm working on finding "THE BEST PRACTICE" when talking about structure of rb files and i the long run working towards making my plugins extensions link this link http://sketchupapi.blogspot.com/2011/07/turn-your-plugin-into-extension.html

      In the attached file I want to make a module and a class even if I ONLY make it a module it fails - I have commented it out again - what fails is my call to the method draw_pulley in the last few lines (unknown).

      If I want this "module and class" structure (to try to do it like "the book") what do I mis then ?

      Best regards
      KSor, Denmark


      What is wrong here WHEN I want this "module & class"-structure

      Best regards
      KSor, Denmark
      Skype: keldsor

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Welcome Keld !!

        See my Ruby Resources and Ruby Newbies Guide to getting Started

        there's a book on my resource list called "Ruby Best Practices"...

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          Dan's guides are very useful...

          A module and a class should start with a Capital letter.
          class Pulley
          Also se @ or @@ variables rather than global $ which will clash with others' scripts unless uniquely named and $entities is not unique at all ! -
          so @@model and @@entities=@@model.active_entities.
          That way you can pass the reference int other methods.***
          Note it's better to use model.active_entities rather than model.entities if the tool might be used inside an edit of a group etc, to ensure the new geometry goes in the right place, and if you are ins the model itself active_entities==entities anyway...
          To add a menu item you must specify the full method/class etc...
          UI.menu("PlugIns").add_item("Kileremskive"){KSOR::Polley.draw_pulley()}
          You could call it as KSOR::Polley.new() and have a standard def initialize() method to start the tool running - you can also pass arguments too like KSOR::Polley.new(123.4) and def initialize(diam=100.0) and then you have 'diam' as say the diameter...
          ***I would actually place the model/entities etc referencing inside the 'initialize' method and not loose in the class.
          Dowmlaod a few other scripts and see how the authors address these issues...

          TIG

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            1. constants are ALL capitals
            2. class and module identifiers need to start with a capital character
            3. class instances need to be constructed by calling their class method new()

            my_pully = Pulley.new()

            1. instance methods need to be called with the reference (var) as the receiver

            my_pully.draw()

            1. module methods must be called from outside with the module identifer as receiver, from inside using self
            • from outside: Keld.start()
            • from inside: self.start()

            You really need to learn the basics of Standard Ruby... before learning the API.
            http://phrogz.net/ProgrammingRuby/frameset.html

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @tig said:

              Dan's guidesa re bery useful...

              :hick:

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                @dan rathbun said:

                @tig said:

                Dan's guidesa re bery useful...

                :hick:
                Corrected it but you were too quick... I was in a rush as I was late for an appointment...

                TIG

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @tig said:

                  @dan rathbun said:

                  @tig said:

                  Dan's guidesa re bery useful...

                  :hick:
                  Corrected it but you were too quick... I was in a rush as I was late for an appointment...

                  Sure.. Sure ... ya were πŸ˜’

                  ... shouldn't hit the stout that heavy, so early in the day. πŸ˜‰

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @Keld: Here's a better way to set platform constants. (I did have a comment on the bottom of the FAQ webpage, showing this, but it's gone on the new Google Developers website.)

                    You can put these outside your author module (namespace,) so they are global constants; or within anyone of your namespaces (modules,) so they are local.

                    MAC = ( RUBY_PLATFORM =~ /darwin/i ? true ; false ) unless defined?(MAC)
                    WIN = ( not MAC ) unless defined?(WIN)
                    

                    Then for conditional expressions:

                    if WIN
                      require('WIN32OLE')
                    else
                      UI.messagebox("Denne facilitet er KUN til Windows !", MB_OK)
                    end
                    

                    String comparison in Ruby is VERY slow:
                    PLAT_SYM.to_s=="windows"
                    Is much slower than testing a boolean constant. Reason is that Ruby must create a new String instance object, whenever it encounters a literal string in your code. (In this example, Ruby must create TWO new String instance objects, one on each side of the == operator. They are abandoned, aka unreferenced, after the line is evaluated. Ruby garbage collection will clean them up eventually. Sooner if the statement was within a method.)

                    It also makes no sense, to assign Symbol constants, and then convert them to a String during a boolean test. It is better (if you absolutly need to use a Symbol,) like:
                    PLAT_SYM == :windows

                    The example on the API FAQ webpage is BAD!
                    PLATFORM is a global Ruby constant (although it is deprecated.) It is now an alias for RUBY_PLATFORM.
                    If you reassign it, you run the risk of breaking other people's plugins. (I have asked Google to fix that page, but ... it's still wrong.)
                    [ This issue is also covered in topic: [Code] FAQ: Detect if plugin is running on the Mac vs PC ? ]

                    Never assign global constants, without first testing if they are already defined.. using a conditional modifier like:
                    unless defined?(*ConstantName*)

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      require 'sketchup.rb'

                      1. This is cross-platform, not Windows only.

                      2. It belongs at the top of the file.

                      3. # pull in the standard API hooks.

                      This NOT true. The API "hooks" are always loaded (if embedded Ruby is loaded,) by the Sketchup executable. (sketchup.exe)

                      1. 'sketchup.rb' adds a few global methods like file_loaded() and file_loaded?(), and does not need to be required, unless you will use methods from that file, in the specific script, you are writing.

                      2. You should read the "Tools/sketchup.rb' file to see what methods it has that you can use.

                      I'm not here much anymore.

                      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