Backporting Ruby features into RGSS?

While reading up on the ruby specs, I discovered a lot of really powerful language features have been added into ruby (now v 2.1.5) since version 1.9.3 (The version RGSS was based off)

RGSS scripters are really missing out, so I wanted to document all the features NOT included in RGSS, that we could benefit from using - and hopefully implement them.

So, here I will attempt to maintain a list of all features currently NOT implemented in RGSS - along with a brief description of their use, and discussion about whether or not the features are possible to backport or reimplement in RGSS.

The problem with that is RGSS won't get an update. The only time the base of RGSS is when they release a new program as a whole, which doesn't happen often. The best you could do to add any features is rewrite them in Ruby 1.9.3 and put them out on the forums, but that's about it.
The problem with that is RGSS won't get an update. The only time the base of RGSS is when they release a new program as a whole, which doesn't happen often. The best you could do to add any features is rewrite them in Ruby 1.9.3 and put them out on the forums, but that's about it.
 
I'm assuming that's how the backport gem works.
That was the exact point of the title.  Backporting the features into rgss, so scripters could make use of them - if possible for the feature.  
Far too often, scripters will have to write copious amounts of code  just to get their modules implemented on classes, without conflicting method names.

for example:

Class Foo def initialize @var = 0 endendmodule Bar def initialize @vars ||= [] endendIf I want to include Bar on Foo, without overriding the behavior, I would have to do this:

class Foo alias :foo_initialize :initialize include Bar alias :bar_initialize :initialize def initialize foo_initialize bar_initialize endendWith Ruby 2.0 Module#include, you would define Module Bar, like this:

module Bar def initialize super @vars ||= [] endendthen, to get the behavior into Foo, you would write the following simple line:

Foo.send🙂prepend, Bar)or

class Foo prepend BarendMuch easier to work with and MUCH easier to manage compatibility issues with RGSS scripts.  

Does anyone disagree?  Would anyone NOT use this feature was backported?  Is there any reason it would be impossible to backport this feature?
I think it'd be very hard to add/backport Module#prepend to RGSS3 as its implementation requires access to CRuby internals and RGSS is closed source.
If I want to include Bar on Foo, without overriding the behavior, I would have to do this:

class Foo alias :foo_initialize :initialize include Bar alias :bar_initialize :initialize def initialize foo_initialize bar_initialize endendWith Ruby 2.0 Module#include, you would define Module Bar, like this:

module Bar def initialize super @vars ||= [] endendthen, to get the behavior into Foo, you would write the following simple line:

Foo.send🙂prepend, Bar)or

class Foo prepend BarendMuch easier to work with and MUCH easier to manage compatibility issues with RGSS scripts.  

Does anyone disagree?  Would anyone NOT use this feature was backported?  Is there any reason it would be impossible to backport this feature?
This.... I WANT this so badly...

Though, instead of add/backporting, I would like to wait for RGSS4 (if it will use Ruby 2.0+) for some reasons.
Far too often, scripters will have to write copious amounts of code  just to get their modules implemented on classes, without conflicting method names.


aMuch easier to work with and MUCH easier to manage compatibility issues with RGSS scripts.  


Does anyone disagree?  Would anyone NOT use this feature was backported?  Is there any reason it would be impossible to backport this feature?
I probably wouldn't use it, mainly because I haven't found any instances where extracting methods out of multiple unrelated classes into a module would be better design.


If someone has used modules extensively in their scripts for a single script though that would be interesting to see where and how they are used.


Most of my scripts do not assume the existence of other scripts and I tend to prioritize that.
1.9.2 is the actual version, but quibbles.

Explain a feature, and I could probably trivially backport a lot of them.
I think it'd be very hard to add/backport Module#prepend to RGSS3 as its implementation requires access to CRuby internals and RGSS is closed source.
True, but the main features can be replicated fairly easily (though wildly inefficient):

class Module def prepend(*mods) mods.reverse.each(&method🙂prepend_features)) end def prepend_features(mod) meta = class << self; self; end return if meta.ancestors.any? { |ancestor| ancestor == mod || ancestor.included_modules.include?(mod) } old_new = meta.method🙂new).unbind subclass = Class.new(self) do; include mod; define_singleton_method🙂new, &old_new.bind(self)) end define_singleton_method🙂new, &subclass.method🙂new)) prepended(mod) end def prepended(mod) end endThis is not the solution, but its a good start.

Edited for slightly better, less pollutant code.
I probably wouldn't use it, mainly because I haven't found any instances where extracting methods out of multiple unrelated classes into a module would be better design.

If someone has used modules extensively in their scripts for a single script though that would be interesting to see where and how they are used.

Most of my scripts do not assume the existence of other scripts and I tend to prioritize that.

RPG::Actor and RPG::Enemy have a lot of common behaviors/uses, but they don't have a common class shared only by eachother.  If you ever want to apply behaviors to both of them, a module would be ideal.

For example, my data levels scripts use modules to add behavior to existing classes - allowing any class to store relation and level data about any other objects (such as skill levels relative to individual actors or enemies).

Even adding simple logging mechanisms to existing classes would be better with a single module applied to each class.
I probably wouldn't use it, mainly because I haven't found any instances where extracting methods out of multiple unrelated classes into a module would be better design.

If someone has used modules extensively in their scripts for a single script though that would be interesting to see where and how they are used.

Most of my scripts do not assume the existence of other scripts and I tend to prioritize that.
I use a lot of modules in my battle system

And not to mention a lot of sprite class inherited from Sprite_Base

Because I don't want to overwrite the super class, module would be idea. It used to patch most of the subclasses

RPG::Actor and RPG::Enemy have a lot of common behaviors/uses, but they don't have a common class shared only by eachother.  If you ever want to apply behaviors to both of them, a module would be ideal.
Same here, I used module to patch RPG::Actor and RPG::Enemy since they have common behaviors

The advantage of using module that if you ever want to change the logic, you don't need to change all the classes. Especially if they don't have same superclass
RPG::Actor and RPG::Enemy have a lot of common behaviors/uses, but they don't have a common class shared only by eachother.  If you ever want to apply behaviors to both of them, a module would be ideal.


For example, my data levels scripts use modules to add behavior to existing classes - allowing any class to store relation and level data about any other objects (such as skill levels relative to individual actors or enemies).


Even adding simple logging mechanisms to existing classes would be better with a single module applied to each class.
Oh, now that you mention it, I sort of just got lazy in those cases and just added it to RPG::BaseItem and thought "hey this automatically applies to classes, equips, AND states, what a bonus."
Something else I've been using is the "Forwardable" module.

Basically, it takes an instance variable and delegates defined method calls to the variable.

For example, to make an object enumerable, you would usually type the following:

class Skills include Enumerable def initialize(actor) @skills = actor.skills end def [](value) @skills[value] end def each(&block) skills.each(&block) endendInstead, Forwardable allows for this implementation:

class Skills include Enumerable def initialize(actor) @skills = actor.skills end def_delegators🙂@skills, :[], :each)endFar less verbose; makes intent much more obvious, and another feature I thought worth discussing.
Another example to treat module as basic module

I made a module for movement. It can be applied to all class that have x and y attributes. It could be Sprite, Window, or your custom class

module Movement def goto(x, y, duration) # Logic for goto x,y here end def slide(x, y, duration) # Logic for slide here end def update super # <-- I want this! update_move end def update_move # Logic for update here endendNow, you could inject the module to the classes like

Code:
class Sprite  include Movementendclass Window  include Movementendclass Custom_Point  attr_accessor :x  attr_accessor :y  include Movementend

Original thread: https://forums.rpgmakerweb.com/threads/backporting-ruby-features-into-rgss.35050/