In ruby the method body can’t access local variables accessed outside of it. Multiple methods can have the same locsal variables names without collisions. If you want to actually point to the same variable you need to use class variables (or class instance variables).
In this case however I think I’d call GetActiveWindow inside the method instead of using a variable since the active window may not have been SketchUp while the firtst code ran (users can switch focus to another window, e.g. to check their mail, while SU starts up).
Also I would recommend you to read a Ruby style guide as your code is often quite hard to read. In Ruby snake_case is used for variables and method names. Using CamelCase makes it appear as a constant, even if the first character is lower case. Also semicolon is redundant and distracting in Ruby.
That’s why I need to save the hwnd when the plugin is first time loaded. To be able to switch it back when I call the functions working with Sketchup root window.
So I will try to create class for this and an instance of it.
I have my own style which makes me better to read the code.
Do you say, that I must create module? When I create class and new instance white the load of the plugin, the instance is removed from memory when the script finished/program reached the end?
Do I need to create folder for one file or can I create module in one file? Create it and then execute it or add it to Sketchup all in one file?
You must use a module whenever you write any ruby code (except code you run from the terminal) to avoid collisions. Look up the documentation for how modules work.
module Bara
extend self # creates both class and module methods
# module constant
HELLO = 'Hello from Barra' unless defined?( HELLO )
# use the constant in any method in your namespace
def say_hi
UI.messagebox( HELLO )
end # say_hi
end #Barra
# Usage from outside of code
# use the method
# Bara.say_hi
# or use the constant
# UI.messagebox( Bara::HELLO )
Take a look at other plugins (there exists already a lot of source code) to learn Ruby and to learn best practices.
Both modules and classes are containers for code (namespaces). You must use one of them to keep your code separate from other plugins. Classes are modules that can be instantiated. If it does not make sense to instantiate something (because there exists only one of that kind), use a module. See John’s answer.
Everything that is not needed anymore is automatically removed from memory. That is also true for instances. Something is not anymore used if there exists no variable referencing it. Learn what scope is: scope is the region of a program where a variable name is valid. Different variable types have different scope. Example:
# No reference → instance can be removed from memory
MyClass.new()
def method1
# Local variable → lives only until the end of the current method; or outside of methods until the end of the scope/script.
my_instance = MyClass.new()
end
module MyModule
# Module variable → lives permanently in the scope of the module.
# Because of this, you must ensure it always has a valid value.
# For example don't set it to nil when another part of the program assumes it is not nil.
@module_variable = 42
def self.method1(value)
@module_variable = value
end
def self.method2()
# It still exists here!
return @module_variable
end
end
No, it still dosn’t work for me. Either the variable or method is undefined or the get method is undefined when I call it from ruby console. The code is longer so I pasted it externally:
To define class methods (or module methods), write “def self.method_name”. Otherwise you are creating class instance methods and there is no such things for modules as they cannot be instanced.
I am blind. I have watched that many times in your plugin but didn’t realized I miss it in my code.
Edit: but wait, the guys above also skipped the def self.methodname syntax…
Edit: I had serious error/mistake in the code bellow:
if RUBY_VERSION < "2.0.0"
require File.join(PLUGIN_ROOT, "ene_viewport_resizer", "Win32API.so")#Bundled with plugin for SU 2013.
else
require "Win32API"#Standard libraries included in SU 2014+.
I remove the end from else block. This resulted in error which I corrected in the way that I have add end just before the end#module block