Can you overwrite an aliased method?

Zoltor

Veteran
Veteran
Joined
Jan 18, 2014
Messages
1,550
Reaction score
211
First Language
English
Primarily Uses
This thread, and the topic in general has me baffled.

This whole concept, is just messy coding, engines don't work well when the coding is messy as hell(you'll lag or worse, crash, just because of coding like this).

To OP:  Just edit the "already" existing alias, or add a slightly different alias, that more or less does the same thing, but in a different way(tie it to a switch or whatnot if you want to toggle the feature on/off in game, and call it a day).

Seriously I can't think of a single reason you would want to override a alias(an entire script sure, but just an alias, no), It's pretty pointless, you don't want it to work the way it is, just change it
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Having one script change another script's logic generally means breaking the other script and therefore you have a script incompatibility.
 

Zoltor

Veteran
Veteran
Joined
Jan 18, 2014
Messages
1,550
Reaction score
211
First Language
English
Primarily Uses
Having one script change another script's logic generally means breaking the other script and therefore you have a script incompatibility.
Naturally, but when I said edit/change, I did infact mean change it in the original script, of course not in this screwed up script, where the OP is trying to hijact the alias in another script.

If the OP wanted to make a supplement script, to work with another script, they should've designed it to work with it, basing the supplement script off of what has already been defined in the other script, you can't just do whatever, and expect it to work.

The alternative would be to recreate that system, with the add-on in mind(or the add-on/supplement script, whatever's easier in X case), which should've been done from the beginning, then there wouldn't even be a need for attempts at such messy coding.

OP, let this be a lesson to you. Before you make matters worse, I suggest you create your new script from scratch, to work with whatever scripts you want it to work with, base it around such, don't fight it.
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
This thread, and the topic in general has me baffled.

This whole concept, is just messy coding, engines don't work well when the coding is messy as hell(you'll lag or worse, crash, just because of coding like this).
It's not messy when done right. It's Ruby coding. Ruby was designed specifically to handle situations like this. It's a very flexible language. In essence, aliasing is Ruby's answer to overloading methods. If you think this is "strange" wait till you learn about all the wonderful things you can do with meta-programming and reflection...

Seriously I can't think of a single reason you would want to override a alias(an entire script sure, but just an alias, no), It's pretty pointless, you don't want it to work the way it is, just change it
See my post on the bottom of the last page. You may lack the imagination or experience to think of a reason, but that doesn't mean reasons don't exist.

I've noticed you tend to think in absolutes (at least when you post here). 

Sometimes you need to step back and see all the shades of gray.
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
OP, let this be a lesson to you. Before you make matters worse, I suggest you create your new script from scratch, to work with whatever scripts you want it to work with, base it around such, don't fight it.
Sorry, but I am sure I stated somewhere that this was to help someone else. Whom has less coding experience and wanted to change the functionality of one script very slightly.

To be more specific, there was one method which has to be removed - I wondered if it would be possible to completely overwrite the alias methods functions to avoid me having to state step my step instructions for which code to remove.

I ended up just informing them of the method to remove and gave them the 'working' code. It was much easier.

But I still think that knowing how to perform such an act - messy or not, as long as its functional and efficient(to an certain), would be helpful in potential situations that could arise.

Knowledge is useful, regardless of how you intent to utilize it.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
Well... having a topic start with a question, which is followed by some code answering that very question, is indeed a bit special.
Yes. You can perfectly fine overwrite an alias, it is nothing special. Dekita, your problem stems from your conceptual model of overwriting and aliasing.

Let's say you have some example code (continuous example, all code pieces are assumed to be put in continuation of each-other)

Code:
# Small example classclass Foo  attr_accessor :my_attribute   def hello    'Hello'  endend# We can call our methodfoo = Foo.newp foo.hello                         #=> Hello
foo.hello simply points to a method. We have on our foo object a method name hello pointing to a method we created. It is perfectly possible to take the method an play around with it:
Code:
# Let's fetch ithello_method = foo.method(:hello)# Look, it can be called!p hello_method.call                 #=> Hello
Overwriting a method simply means to make the method name point to another method. That's it.
Code:
# Overwrite -> Create a new method and let :hello method point to itclass Foo  def hello    'World'  endend# Yup, an overwritten methodp foo.hello                         #=> World# We can still call the old methodp hello_method.call                 #=> Hello
Of course, we didn't have a reference to the method it will eventually be destroyed by the garbage cleaner. Since we have a reference, let's play around with it instead:
Code:
# But what about the old method?foo.my_attribute = hello_method# Yup, we can still use itp foo.my_attribute.call             #=> Hello
Yup, a method is an object, so we can treat it as one.
Now that we understand a bit about methods and overwrites let us take a look at aliasing:
Code:
# Ok, let us try some aliasingclass Foo  alias :world :helloend# Let us try calling themp foo.hello                         #=> Worldp foo.world                         #=> World# Yup, they are in fact pointing to the same elementp foo.method(:hello) == foo.method(:world) #=> truep hello_method == foo.method(:world) #=> false
Look at that, alias simply gives us another method name pointing to the same method object. Aliasing does not copy methods. True, when you give a method more than one name you can still access it if one of the names is overwritten.
To give some extra information we can do the same in a different way. Hopefully this helps your understanding:
Code:
# Let us put the old method back with a little hackclass Foo  def create_method(method_symbol, method)    self.class.send(:define_method, method_symbol, method)  endendfoo.create_method(:hello, hello_method)# What happens when we call the method again?p foo.hello                         #=> Hellop foo.method(:hello) == foo.method(:world) #=> false# No need for any pesking aliasfoo.create_method(:hello_take_two, hello_method)p foo.hello_take_two                #=> Hellop foo.method(:hello) == foo.method(:hello_take_two) #=> true
For more information on this subject I highly recommend The Ruby Object Model by Dave Thomas


 
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,078
Members
137,580
Latest member
Snavi
Top