Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Alias Guide

Alias Guide by vgvgf


Alias Definition
An alias, is a pseudonym, a nickname, of something. In ruby, this something can be an existing method or global variable. Constants(Variables, classes and modules), local variables, instance variables and class variables can't be aliased.

Sintaxis
Creating a new alias can be performed in two different ways:
- alias pseudonym source
- alias_method(pseudonym, source)
The alias_method way, only can be used for methods of a module type, and the pseudonym, and the source, must be symbols.

Note: alias_method can be used in classes, since Class, is a subclass of Module.
Note: Symbols are defined, using ':' at the start of the object. Example: :thisIsASymbol. Strings can be converted into symbols, using the method to_sym.

How it work
Code:
class Numeric
  alias plus +
end
a = 10
p a + 10         => 20
p a.plus(10)     => 20
In this example the operator '+' of the numeric class, is aliased with the new 'plus' pseudonym.

Note for the ones that don't know: '+' is a method, so you can use it like:
Code:
p 10.+(20) => 30
When a method is aliased, the new name refers to a copy of the original method's body. So if you modify the source method, the aliased method will not be modified.

You can alias global variables, too. Example:
Code:
$test = 'Bye World!'
alias $new_globalvariable $test
p $new_globalvariable            => "Bye World!"
Continuing with the last example:
Code:
$test = 'Hello World!'
p $new_globalvariable            => "Hello World!"
You can see that in this case, when the source variable is edited, the aliased is edited too. In the same way, editing the source global variable, will make the aliased global varible to be modified. So aliasing a global variable will make the aliased global variable refer the source global variable.

Applicable Uses
Adding code to methods
Code:
class Foo
  def test
    p 'A'
  end
  alias old_test test
  def test
    old_test
    p 'B'
  end
end
a = Foo.new
a.test
Will produce:
Code:
"A"
"B"
In this example, first is defined the method test with the function p 'A', so
calling it will make a popup with the 'A' text.
When the alias function is used, the method test is copied, and named old_test. That make the class Test to be like:
Code:
class Foo
  def test
    p 'A'
  end
  def old_test
    p 'A'
  end
end
And at the end, it is redefined the method test, where there is called the method old_test, and then it is called a popup with the text 'B'. The class test would be like:
Code:
class Foo
  def test
    old_test
    p 'B'
  end
  def old_test
    p 'A'
  end
end
Also, you can do things like:
Code:
class Foo
  def test
    old_test
    p 'B'
    old_test
  end
end
a = Foo.new
a.test
Will produce:
Code:
"A"
"B"
"A"
Now, what happend when the old_method has arguments input, you will need to input it the arguments.
Code:
class Foo2
  def test(text)
    p 'A' + text
  end
  alias old_test test
  def test
    old_test('ZXY')
    p 'B'
  end
end
a = Foo2.new
a.test
Will produce:
Code:
"AZXY"
"B"
Combining two methods
Code:
class Foo3
  def testA
    p 'A'
  end
  def testB
    p 'B'
  end
  alias old_testA testA
  def testA
    testB
    old_testA
  end
end
a = Foo3.new
a.testA
Will produce:
Code:
"B"
"A"
In this example, there is defined two methods, test3A and test3B, each one makes a popup with the letter A for test3A and letter B for test3B. Then it is aliased the method test3A with old_test3A. And at the end, it is redefined the method test3A, who calls method test3B and old_test3A.

Making two methods the same
Code:
class Foo4
  def test1
    p 1
  end
  def test2
    p 2
  end
  alias test2 test1
end
a = Foo4.new
a.test1
a.test2
Will produce:
Code:
1
1
Here, when you alias the method test1 with test2, it's redefinded the method test2, with the code of method test1.
If you want to combine and make the same two or more methods, you can do:
Code:
class Foo5
  def test1
    p 1
  end
  def test2
    p 2
  end
  def test3
    p 3
  end
  alias old_test1 test1
  alias old_test2 test2
  alias old_test3 test3
  def test1
    old_test1
    old_test2
    old_test3
  end
  alias test2 test1
  alias test3 test1
end
a = Foo5.new
p 'Testing test1'
a.test1
p 'Testing test2'
a.test2
p 'Testing test3'
a.test3
Will produce:
Code:
"Testing test1"
1
2
3
"Testing test2"
1
2
3
"Testing test3"
1
2
3
Firts is defined the methods test1, test2, test3. Then is aliased the 3 methods with old_test1, old_test2 and old_test3. Here the class Foo5 is like:
Code:
class Foo5
  def test1
    p 1
  end
  def test2
    p 2
  end
  def test3
    p 3
  end
  def old_test1
    p 1
  end
  def old_test2
    p 2
  end
  def old_test3
    p 3
  end
end
Then is redefined method test1, who calls methods old_test1, old_test2 and old_test3. Here the class Foo5 is like:
Code:
class Foo5
  def test1
    old_test1
    old_test2
    old_test3
  end
  def test2
    p 2
  end
  def test3
    p 3
  end
  def old_test1
    p 1
  end
  def old_test2
    p 2
  end
  def old_test3
    p 3
  end
end
Then is redefined methods test2 and test3 with a copy of method test1, with the alias function. Finaly the class Foo5 is like:
Code:
class Foo5
  def test1
    old_test1
    old_test2
    old_test3
  end
  def test2
    old_test1
    old_test2
    old_test3
  end
  def test3
    old_test1
    old_test2
    old_test3
  end
  def old_test1
    p 1
  end
  def old_test2
    p 2
  end
  def old_test3
    p 3
  end
end
Tips
Making private new methods from alias
Using the last example, with the class Foo5, you can do things like:
Code:
a = Foo5.new
a.old_test1     => 1
a.old_test2     => 2
a.old_test3     => 3
If you want prevent this, you can use the private method.
Code:
class Foo5
  private(:old_test1, :old_test2, :old_test3)
end
Now if you try:
Code:
a = Foo5.new
a.old_test1
Will raise a NoMethodError class Error.

Aliasing Modules
Sometimes, a module has its methods defined by self.method. Example:
Code:
module TestModule
  def self.test
    p 'test'
  end
end
You cannot alias the method self.test directly, because the method test is from the class self of the module TestModule. So for aliasing it you need to do:
Code:
module TestModule
  class << self
    alias new_test test
  end
end
TestModule.new_test
Will produce:
Code:
"test"
Avoid stack when aliasing hidden classes
When you press F12, while playing or testing a rmxp game, it will restart. When the game restarts, all the classes and its changes are conserved. Only for hidden classes, this bug is given. For example, when you start the game, and you make this alias for adding code to Math.sin:
Code:
module Math
  class << self
    alias_method(:old_sin, :sin)
    def sin(args)
      args += 1
      old_sin(args)
    end
  end
end
This change will make the module Math to be like:
Code:
module Math
  def self.sin(args)
    args += 1
    old_sin(args)
  end
  def self.old_sin(args)
    [ ... code ... ]
  end
end
When the game is restarted, the code will redo the alias operation, but the new Math.sin method will be copied to Math.old_sin, and module Math will be like:
Code:
module Math
  def self.sin(args)
    args += 1
    old_sin(args)
  end
  def self.old_sin(args)
    args += 1
    old_sin(args)
  end
end
That, makes the method Math.old_sin to be circular, so it will recall self forever without stopping. That makes an SystemStackError. So calling Math.sin, who calls Math.old_sin, will produce that error.

To avoid this, you can use:
Code:
module Math
  class << self
    if @alias_old_sin.nil?
      alias_method(:old_sin, :sin)
      @alias_old_sin = true
    end
    def sin(args)
      args += 1
      old_sin(args)
    end
  end
end
So, the aliasing will only be maked the first time called.

Also, you can use this method (that I have created):
Code:
#==============================================================================
# ** class Module
#==============================================================================
class Module
  #--------------------------------------------------------------------------
  # * Alias Method
  #--------------------------------------------------------------------------
  alias_method(:vgvgf_aliasmethod, :alias_method)
  def alias_method(newName, oldName, prevent_stack = false)
    return if self.method_defined?(newName) and prevent_stack
    vgvgf_aliasmethod(newName, oldName)
  end
end
You will need to use alias_method ever, when aliasing hidden classes, and include a 3th argument with true value, for use the prevention. Example:
Code:
module Math
  class << self
    alias_method(:old_sin, :sin, true)
    def sin(args)
      args += 1
      old_sin(args)
    end
  end
end
 

rutix

Member

vgvgf;194338 said:
See: "Applicable Uses" section, specialy "adding code to a method", that is the most usable intention.
ok lets say it this way i never know when to use it. Can you give a couple examples?
 
ok lets say it this way i never know when to use it. Can you give a couple examples?

Example 1
You want to add code to a hidden class. In this example, to the Graphics.update class, for making this class to update the Input class. You don't know what is the code for this method, because it is hidden, so you can't overwrite directly. So you need to alias it.
Code:
module Graphics
  class << self
    alias_method(:aliased_graphics_update, :update)
    def update
      Input.update # Here Input is updated
      aliased_graphics_update # Here the original Graphics.update method is called
    end
  end
end
Example 2
You want that yours script, be compatibly with other scripts. Your script need to modify the update method in the Game_Player class. This is a very common modified method, so if you overwrite directly this method, maybe will cause problems with other scripts. So you can alias this method to add your code, and the others, too. So all scripts can be compatible.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top