Well as you see here, it causes confusion in reading your code. It is best to always use parenthesis in method definitions, and when calling all methods except global methods (those defined in Kernel
, BasicObject
, Object
, Module
and Class
.)
Omitting parenthesis and using non-standard indentation causes misread (as above) and we “spin out wheels” trying to help you.
A mixin is not (usually) the way to expose an instance state variable.
Probably better would be just what we call a wrapper method in a outer module. The module holds a @instance
reference to the instance, the module defines a wrapper method that calls the instances attribute reader method, and the command calls the module’s wrapper method.
Where the Namespace
module is already predefined. (Ie, saving an ident here.)
module Namespace::SomeExtension
extend self
CMD ||= {} # Hash to hold UI command references
@instance = nil unless defined?(@instance)
class TestClass
attr_reader :state
def initialize( state )
@state = state
end
def switch_state
@state = @state == 0 ? 1 : 0
end
end # end of class
def get_state
@instance ? @instance.state : 0
end
def create_instance(state = 0)
@instance = TestClass.new(state)
end
def toggle
@instance.switch_state if @instance
end
if !defined(@ui_loaded)
@ui_loaded = true
# Definition of UI objects
CMD["Toggle"] = UI::Command.new("Toggle Test") {
toggle()
}.set_validation_proc {
get_state() == 0 ? MF_UNCHECKED | MF_CHECKED
}
# Perhaps more command objects here ?
# Define and load the toolbar:
TOOLBAR = UI.toolbar("Testbar")
CMD.each { |key,cmd|
TOOLBAR.add_item(cmd)
}
end
end