• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Ruby Scripting Question - Defining Functions

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 3 Posters 169 Views
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
    jrstoup
    last edited by 11 Jan 2012, 18:26

    I am trying to streamline my code by putting my function calls into a loop. So far, I have been unsuccessful. Anyone care to give me a clue as to why?

    #What I have
    UI.menu("PlugIns").add_item("Method 1") {method1}
    UI.menu("PlugIns").add_item("Method 2") {method2}
    UI.menu("PlugIns").add_item("Method 3") {method3}
    
    def method1
      derp = "derp1"
    end
    
    def method2
      derp = "derp2"
    end
    
    def method3
      derp = "derp3"
    end
    

    The above code creates three menu items called "Method 1", "Method 2" and "Method 3". Those entries call the corresponding methods created below them. What I would like to do is put all of that into a loop so I can create as many entries and methods as I want. Here is my rough attempt at that:

    #What I want
    max = 3
    for n in 1..max
      switchStr = ""
    
      UI.menu("PlugIns").add_item("Method " + n.to_s) {
        switchStr = "method" + n.to_s
        eval switchStr
      }
    
      def switchStr
        derp = "derp" + n.to_s
      end
    
    end
    

    Now, unfortunately the above code doesn't work. I am currently looking through the Ruby documentation to determine what exactly it is that I need (a proc perhaps?) however I thought I would take a shortcut and ask you. So, what am I doing wrong here? I feel like this should be simple and I'm just missing something. Thoughts?

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 11 Jan 2012, 19:00

      Why not make one method that takes arguments

      module James
      
      ###menu
      plugins=UI.menu("PlugIns")
      plugins.add_item("Some_Method 1"){self.some_method(1)}
      plugins.add_item("Some_Method 2"){self.some_method(2)}
      plugins.add_item("Some_Method 3"){self.some_method(3)}
      
      ### method[s]
      def self.some_method(arg=0)
        @derp="derp"+arg.to_s
        ### etc
        self.another_method() if arg > 0
      end
      
      def self.another_method()
        UI.messagebox("You chose '"+@derp"' !")
      end
      
      end#module
      

      Put your methods inside their own [your?] Module!
      Called as James.some_method(arg)
      The value of 'arg' can be any type of class - here it defaults to 0, and the .to_s assumes we want to convert it, whatever it is, into a string...
      Note, making derp into @derp will allow you to use it in other methods within the same module too !

      TIG

      1 Reply Last reply Reply Quote 0
      • J Offline
        jrstoup
        last edited by 12 Jan 2012, 17:06

        I tried using your function and sadly, regards of which menu item I select ("Some_Method 1", "Some_Method 2" or "Some_Method 3") it always says "You chose 3". Any ideas why I am only getting the final index of the loop?

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 12 Jan 2012, 18:49

          Without seeing your exact code I don't know... 😕

          TIG

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 13 Jan 2012, 03:08

            The first line in TIG's example should read:

            module JStroup

            and the last:

            end #module

            He typo'd ("method" instead of "module". And I'd use a more unique module name than just "James", but TIG's intent is that you always have your code run inside your own unique "Author" namespace.)

            P.S.: This thread should be moved to the Developers forum.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 13 Jan 2012, 03:20

              @jrstoup said:

              I tried using your function and sadly, ... it always says "You chose 3". Any ideas why I am only getting the final index of the loop?

              It may work better if you pass the argument on to the another_method(), like:
              (Probably the cause is really the "module" typo as above.)

              require('sketchup.rb')
              
              module JStroup
              
                ### method[s]
                def self.some_method(arg=0)
                  @derp="derp"+arg.to_s
                  ### etc
                  self.another_method(arg) if arg > 0
                end
               
                def self.another_method(arg)
                  UI.messagebox("You chose '#{arg.to_s}' !")
                end
               
              
                ### load once block
                unless file_loaded?(File.basename(__FILE__))
                  ### menu
                  plugins=UI.menu("PlugIns")
                  plugins.add_item("Some_Method 1"){self.some_method(1)}
                  plugins.add_item("Some_Method 2"){self.some_method(2)}
                  plugins.add_item("Some_Method 3"){self.some_method(3)}
                  ### reqister file as loaded
                  file_loaded(File.basename(__FILE__))
                end
               
              end#module
              

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 13 Jan 2012, 10:46

                Sorry about the typo !
                I've corrected the original post 😳
                [== a brain-fart - I must must type 'module' several times a day !]

                TIG

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

                Advertisement