07-14-2023, 08:08 PM
(This post was last modified: 07-14-2023, 08:16 PM by DerVVulfman.)
Well... that was an example of incorrectly using the 'Include' private method (or command) that is built into Ruby's Module class. The Include method allows one to invoke (or duplicate) features from another targeted object. This object typically a separate Module or Class.
However, the example I provided and you are linking was one of recursion. Not of simple nesting.
I could nest a class within a module, which is commonplace as part of Object Language's hierarchal structure. Consider RPGMaker's RPG::Actors class, an actor class within the RPG module. In 'general' for object orientated code, statements and code may be written and contained within methods. A collection of methods may be contained within a class. And classes may be contained within Modules.
This is the same hierarchy within C++, but more rigidly enforced.
I could also nest a class within another class, or even use 'include' to nest a module within a class. This would not generally cause a problem, but looks ugly.
An functional example of successfully nesting a method within another method.
It makes little sense to do actually do such, and the 'nested' method cannot be called elsewhere but the parent method it is contained. But coding is a matter of logistics and flow.
And I did test it with the "\c[#]" control character command within Show Choice as 'text_color' is invoked in the Window_Message class.
But the example in which you are citing is where I described the improper use of the Include command to create an endless loop... recursion... because the 'include' command placed a copy of the entire module within the class that was already within the module. Since the module was now within the class, that meant the class was now within the module within the class... ad infinum.
And the example of mine which you cited was an example or recursive flow, as dangerous an issue as stack overflow.
However, the example I provided and you are linking was one of recursion. Not of simple nesting.
I could nest a class within a module, which is commonplace as part of Object Language's hierarchal structure. Consider RPGMaker's RPG::Actors class, an actor class within the RPG module. In 'general' for object orientated code, statements and code may be written and contained within methods. A collection of methods may be contained within a class. And classes may be contained within Modules.
This is the same hierarchy within C++, but more rigidly enforced.
I could also nest a class within another class, or even use 'include' to nest a module within a class. This would not generally cause a problem, but looks ugly.
An functional example of successfully nesting a method within another method.
Code:
#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
# This class is for all in-game windows.
#==============================================================================
class Window_Base < Window
#--------------------------------------------------------------------------
# * Get Text Color
# n : text color number (0-7)
#--------------------------------------------------------------------------
def text_color(n)
#
#------------------------------------------------------------------------
# * A color selection method 'nested' in another method (IT WORKS)
# n : text color number (0-7)
#------------------------------------------------------------------------
def text_nested_color(n)
case n
when 0
return Color.new(255, 255, 255, 255)
when 1
return Color.new(128, 128, 255, 255)
when 2
return Color.new(255, 128, 128, 255)
when 3
return Color.new(128, 255, 128, 255)
when 4
return Color.new(128, 255, 255, 255)
when 5
return Color.new(255, 128, 255, 255)
when 6
return Color.new(255, 255, 128, 255)
when 7
return Color.new(192, 192, 192, 255)
else
normal_color
end
end
#------------------------------------------------------------------------
# * End of Nested Method
#------------------------------------------------------------------------
#
# I am calling a method IN this method
return text_nested_color(n)
#
end
end
And I did test it with the "\c[#]" control character command within Show Choice as 'text_color' is invoked in the Window_Message class.
But the example in which you are citing is where I described the improper use of the Include command to create an endless loop... recursion... because the 'include' command placed a copy of the entire module within the class that was already within the module. Since the module was now within the class, that meant the class was now within the module within the class... ad infinum.
And the example of mine which you cited was an example or recursive flow, as dangerous an issue as stack overflow.