• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Access module variables from inside a module class

Scheduled Pinned Locked Moved Developers' Forum
2 Posts 2 Posters 307 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.
  • S Offline
    sepultribe
    last edited by 30 Jul 2015, 20:13

    I have the following code

    module MyModule
    
    @@settings_hash = {}
    
    if File.exists?(SETTINGS_PATH)
      @@settings_hash = YAML;;load_file SETTINGS_PATH
      puts @@settings_hash
    end
    
    def self.get_settings(hash_key)
      @@settings_hash.[](hash_key.to_sym)
    end
    
    ...
    ...
    @@settings_hash gets used here inside several function blocks
    ...
    
    class SensorNodeInfoTool
    
    ...
    ...
    
    def m_get_set(string)
        MyModule.get_settings(string)
    end
    
    def onLButtonDown(flags, x, y, view)
      ...
      ...
        if m_get_set("ws_type") == 'NOWSDL'
          client = Savon.client(endpoint; m_get_set("endp"), namespace; m_get_set("ns"))
        elsif m_get_set("ws_type") == 'WSDL'
          client = Savon.client(wsdl; m_get_set("doc"))
        end
      ...
      ...
    
    

    The problem is using Module @@ or @ variables inside a Class that is inside the same Module, like my SensorNodeInfoTool there. I don't think I have found a way to access @variables yet (I think those are called Module Instance variables). The above code has @@variables and I have to use a the module namespace AND set a reader method, and I have also used a second function inside the Class to not have to use the Module namespace prefix. Is there another more proper way? I have the feeling that I might be doing it wrong.

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 31 Jul 2015, 01:34

      "Programming Ruby - The Pragmatic Programmer's Guide" explains it all.

      I'm not here much anymore.

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

      Advertisement