Understanding Aliases

Started by Jaiden, November 08, 2017, 01:56:38 pm

Previous topic - Next topic

Jaiden

I've done some reading on aliases and understand (somewhat) how they work, but there is still something I don't understand:

When looking at an alias method, I've noticed the method appears within the alias, but I'm not sure I correctly understand it's function in relation to its placement, as I've seen it both ways. For example:

alias superduper_update_target update_target
def update_target
   #some code
   #goes here
   superduper_update_target
end


vs

alias superduper_update_target update_target
def update_target
   superduper_update_target
   #some code
   #goes here
end


Does this determine where in the original "update_target" method the code is placed? In this case, the first method places the new code above the original method, and the second method places the new code below the original method, or is it visa versa?

Thanks guys.






KK20

QuoteIn this case, the first method places the new code above the original method, and the second method places the new code below the original method

This is correct.
Order is everything:

def foo(a, b)
  @a = a
  @b = b
end

# Works
alias bar foo
def foo(a, b)
  bar(a, b)
  @a += 5
end

# Syntax Error
alias bar foo
def foo(a, b)
  @a += 5 # @a is currently nil
  bar(a, b)
end

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Jaiden