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
Note for the ones that don't know: '+' is a method, so you can use it like:
Code:
p 10.+(20) => 30
You can alias global variables, too. Example:
Code:
$test = 'Bye World!'
alias $new_globalvariable $test
p $new_globalvariable => "Bye World!"
Code:
$test = 'Hello World!'
p $new_globalvariable => "Hello World!"
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
Code:
"A"
"B"
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
Code:
class Foo
def test
old_test
p 'B'
end
def old_test
p 'A'
end
end
Code:
class Foo
def test
old_test
p 'B'
old_test
end
end
a = Foo.new
a.test
Code:
"A"
"B"
"A"
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
Code:
"AZXY"
"B"
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
Code:
"B"
"A"
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
Code:
1
1
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
Code:
"Testing test1"
1
2
3
"Testing test2"
1
2
3
"Testing test3"
1
2
3
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
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
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
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
Code:
class Foo5
private(:old_test1, :old_test2, :old_test3)
end
Code:
a = Foo5.new
a.old_test1
Aliasing Modules
Sometimes, a module has its methods defined by self.method. Example:
Code:
module TestModule
def self.test
p 'test'
end
end
Code:
module TestModule
class << self
alias new_test test
end
end
TestModule.new_test
Code:
"test"
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
Code:
module Math
def self.sin(args)
args += 1
old_sin(args)
end
def self.old_sin(args)
[ ... code ... ]
end
end
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
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
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
Code:
module Math
class << self
alias_method(:old_sin, :sin, true)
def sin(args)
args += 1
old_sin(args)
end
end
end