12-29-2024, 10:19 PM
(This post was last modified: 12-29-2024, 10:20 PM by DerVVulfman.)
(12-21-2024, 06:40 PM)DerVVulfman Wrote: Within RUBY... even available when using RPGMaker XP's RGSS... you have access to the OBJECT class. Not that you see it in the Scripts database. And the OBJECT class has a few methods. One of these methods is 'instance_variables' which would return an array listing all the variables specific for the class within.
However, different versions of Ruby return this array differently.
With Ruby v 1.8.1 (used by both RPGMaker XP and VX at the very least), the names stored within the returned array are listed as strings:
. . .
But at some point, RUBY had changed, and the current versions (such as version 2.7 used by HiddenChest) now return the instance variables as their 'symbols', and not by their names as strings.
Indeed, I am returning to this. For a learned colleague did suggest there are advantages with using symbols instead of strings for variable names. However, there are disadvantages too.
Within the below code block, we see the beginning of a loop that acquires a list of active objects created within the class, mainly instance variables for windows and so on.
Code:
# Passes Through All Instance Variables
self.instance_variables.each do |object_name|
# Ensure we have a string representation of the object
p object_name
p 'hash' if object_name.is_a?(Hash)
p 'Array' if object_name.is_a?(Array)
p 'disposable' if object_name.respond_to?(:dispose)
p 'disposable' if object_name.respond_to?(:update)
BUT...
Code:
# Passes Through All Instance Variables
self.instance_variables.each do |object_name|
# Ensure we have a string representation of the object
obj_string = (object_name.is_a?(String)) ? object_name : object_name.to_s
# Evaluates Object
object = eval obj_string
p object_name
p 'hash' if object.is_a?(Hash)
p 'Array' if object.is_a?(Array)
p 'disposable' if object.respond_to?(:dispose)
p 'disposable' if object.respond_to?(:update)
Note that you cannot evaluate symbols into their original objects as the second code block has shown. the 'eval' command only works with strings. And the .respond_to? command is a method within the Object class and will not respond to symbols.