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

Sharing variables between functions

Scheduled Pinned Locked Moved Developers' Forum
3 Posts 2 Posters 158 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.
  • K Offline
    kybasche
    last edited by 18 Aug 2012, 21:30

    Hey all.

    One day, I'll get good enough at this to offer help instead of only asking question.
    But for now:

    I would like to be able to use variables from one function inside a different function. Please forgive my poor scripting - but as an example...

    module Plugin
    
    unless file_loaded?("program.rb")
    mymenu = UI.menu('Plugins').add_submenu('test')
    mymenu.add_item('Test Program') {self.Box}
    mymenu.add_item('Test Program 2') {self.Circle}
    file_loaded("program.rb")
    end #unless
    
    def self.Box()
    a = 5
    b = 6
    c = a * b
    end #Box
    
    def self.Circle()
    d = 12
    e = d * a ##this won't work of course, because (a) is not defined within this function... how do you pull the value of (a) from Box?
    end #failCircle
    
    end #module
    

    Thanks much!

    Derek

    1 Reply Last reply Reply Quote 0
    • C Offline
      Chiefwoodworker
      last edited by 18 Aug 2012, 21:53

      Hi Derek,

      Make variable a a class instance variable e.g.@a.

      def self.Box()
      @a = 5
      b = 6
      c = @a * b
      end #Box

      def self.Circle()
      d = 12
      e = d * @a

      end #module

      Joe....
      http://www.srww.com

      1 Reply Last reply Reply Quote 0
      • K Offline
        kybasche
        last edited by 18 Aug 2012, 22:22

        amazing how easy things are sometimes.

        guess i have a long way to go 😄

        thanks!

        D

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

        Advertisement