[Ruby] Simplest access to a class instance?

For general discussion related FlowStone
Post Reply
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

[Ruby] Simplest access to a class instance?

Post by tulamide »

Scenario:
3 RubyEdits connected to views, each view displays a part of a whole. There's one class that manages everything, defined and instantiated in the first of the 3 modules.

First Implementation:
I used a class variable for accessing the class instance from all 3 RubyEdits. Works like a charm. However, let me remind you that all instances of a plugin share the same Ruby Interpreter. Therefore, if working with several instances of this schematic, any instance overwrites the class for all other instances as well.

Question:
I tried to use class instance variables instead and send the class instance out via RubyValue. Unfortunately that creates copies of the class instance, instead of letting me access the instance itself. How can I have access to one class instance from all 3 modules, while still have individual class instances per plugin instance? Do I really need to implement a callback system?
"There lies the dog buried" (German saying translated literally)
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: [Ruby] Simplest access to a class instance?

Post by KG_is_back »

You can create your class as a module. Then create Anonymous class in each plugin and include the module in it. Then send reference to your anonymous class to each ruby-edit in that plugin during startup. This would ensure that each plugin has its own instance of that class.

https://www.mobomo.com/2012/2/using-anonymous-classes-and-modules-in-ruby/
Post Reply