Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Ruby Object compare and get changed values

For general discussion related FlowStone

Ruby Object compare and get changed values

Postby chackl » Tue Jan 14, 2014 10:50 am

Hello!

Does anybody know a function that is able to compare 2 cloned objects and lists all values that has been changed in one of them?

Regards
100% accuracy is the guarantee to your success. The value alters if you combine it with musical creativity.
User avatar
chackl
 
Posts: 233
Joined: Tue Aug 17, 2010 8:46 pm
Location: Austria / Salzburg

Re: Ruby Object compare and get changed values

Postby trogluddite » Tue Jan 14, 2014 9:00 pm

Well, Ruby has good powers of 'reflection' - i.e. finding out metadata about objects and the code that is running.
The classes "BasicObject", "Object" and "Kernel" are the ones to check out here, as those are the 'foundation' classes from which nearly all others are descended. That means that the methods of those classes will work on almost any object - check out the API documents for full details, but here are a few that you may find useful...

.instance_variables
Gets an array containing the names of all the object's instance variables. The names are in the form of Symbols, which is handy, because...

.instance_variable_get(Symbol)
Returns the value of the instance variable named by the Symbol - even if that variable does not have any 'accessor' methods defined (it breaks 'encapsulation').

So for example...
Code: Select all
# Get a hash of instance variable values for an object
def get_inst_vars(obj)
  vars = obj.instance_variables
  hash = {}
  vars.each{|v| hash[v] = obj.instance_variable_get(v)}
  hash
end

...comparing two such hashes would give a pretty good feel for how 'different' the two objects are. However with 'primitive' objects like Strings and Arrays, there aren't any instance variables as such, so in practice you just have to use the normal comparison methods for those.

Some others that I've always found handy - they're particularly useful if you want to know what methods you can use on a object if you're unsure how to tackle a problem. I always check them out before going to the API documents to see what a method does...

.class
Return the name of the class of which the object is an instance (as a Symbol)

.ancestors
Return an array of the whole "family tree" of an object - all the way back to "BasicObject" from which everything inherits. An object will respond to all of the methods defined in any of its ancestor classes - if two classes have the same method defined, the one from the "nearest relative" wins out.
A good example is Arrays - they have quite a few methods for going through all the array items and doing something (Enumerating). But class 'Array' also inherits "Enumerable" which has many more.

.methods
Get an Array of Symbols of all the methods to which an object responds. These will only be those those methods that are "public" - i.e. accessible from outside the object - though you can see the hidden ones using ".private_methods".

.respond_to?(method_name)

Find out true/false if the object has a particular method.

.__id__

Gets an object's unique instance ID - can help if you have two different objects that just happen to look the same because they contain the same data fields.

.eql?(other)
See if two items are the same object. This is not like "==", which only tellls if the values are the same - '.eql?' is only true if they are the exact same object.

These methods will also work on whole Classes and Modules too - because Classes are also Objects - of class "Class" - which is a sub-class of a class called "Module" - which is a sub-class of "Object" - which is itself also an object of class "Class" - which....
...erm, I'll just stop right there. The rule is really so simple - everything is an object and belongs to a class - but the consequences are not always easy on the human mind! ;)
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby Object compare and get changed values

Postby chackl » Tue Jan 14, 2014 9:54 pm

Well I allready got this fealing that this would have to made "by Hand" :/

OK - So i Extended the Syntax of the rubyviewer to look up all objects inside till there are only base-classes left.
Then an original not modified object that was duplicated by Marshal class is then looked up and it compares each base object and send only thoe that were changed (including some operations for Hash and Array)

Now - I put one Ligt-Definition Object for my liightsoftware inside... and executed this code 10 times a second (Low Frequ data) - And HORRIBLE :O

Could you maybe take a look to it for some code-tweaks for better cpu usage? (Fuction list_changes)

Regards
Attachments
objectviewer+objchange.fsm
Test-Schematic - do not use it now since it is not stable
(6.99 KiB) Downloaded 765 times
100% accuracy is the guarantee to your success. The value alters if you combine it with musical creativity.
User avatar
chackl
 
Posts: 233
Joined: Tue Aug 17, 2010 8:46 pm
Location: Austria / Salzburg

Re: Ruby Object compare and get changed values

Postby trogluddite » Wed Jan 15, 2014 9:51 pm

That's a tricky problem for large objects - no doubt some efficiencies could be found with much benchmark testing, but I don't see anything fundamentally wrong with your code.

It might help to understand your objective a little better.
For example, if you want to make a general purpose debugging tool, then your current approach is appropriate - OTOH, if you only need to know how a specific lighting object has been edited, it might be better to log the changes as they happen as an instance variable of the object. As a general rule in programming, you can nearly always make a routine with a specific target more efficient than something "general purpose" - but that might not fit your needs at all.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK


Return to General

Who is online

Users browsing this forum: No registered users and 74 guests