Can you overwrite an aliased method?

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
What is your actual problem? What are you trying to do?

Seems to me that in generalizing the problem you are attempting to use aliases to build something on fluffy clouds of uncertainties.

I can see that Tsu also don't really know what problem you are trying to solve, so perhaps focusing on the concept you are mentioning will help.

Do you know what it means to overwrite a method?

Do you know what it means to alias a method?

Or perhaps rather, how are imagining these two concept work?

*hugs*

 - Zeriab
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Yeah, I think we can solve this faster if we focus on what you need it to do...
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
Yeah, I think we can solve this faster if we focus on what you need it to do...
At this point I think there's more than enough posts to warrant splitting it off into its own thread...
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
Yea agreed. However, this isnt really a problem - or something I am still trying to achieve. I was merely wondering if its possible to overwrite an alias method - without modifying the original method.

Do you know what it means to overwrite a method?

Do you know what it means to alias a method?
Overwrite = to overwrite all previous definitions of [x] method within [x] class with your own definition.

Alias = allows you to essentially give a method a second name - the alias name. This allows you to change the initial definition of method [x] within class [x] - used if you wanted to add something to the orig method before / after the orig method is called - which is called using the new name that you gave to the method, when you aliased it.

Once I can properly word the question, with accurate test code for it - i will begin a new thread. Until then, it may be best to allow this thread to 'move on' ^_^
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
If you overwrite the alias, then you're not really modifying the original method... But of course, you'll probably lose the ability to call the original method without causing an infinite loop

other than that:

if you want to be able to still use the original method, better yet just make the new script alias the method, then edit your older script in such a way that it's alias will only work if the new script is non existent

 
#New Script$imported["DEKITA"]=truealias dekita2 datadef dataend#old scriptif not $imported["DEKITA"] alias dekita1 data def data endendand put the new script above the old one...
or

Code:
#old scriptalias dekita1 datadef data  if $imported["DEKITA"]    dekita1    #so that it skips the new methods if the newer script is here    return  end  #Your things here for the old scriptend
then you can just put the new script below it
Code:
#new script$imported["DEKITA"] = truealias dekita2 datadef data  dekita2  #Your new method's stuff hereend
That will probably be better than trying to overwrite the alias
 
Last edited by a moderator:

ShadowLurk

Tanoshii~
Veteran
Joined
Feb 14, 2014
Messages
226
Reaction score
53
Primarily Uses
If you overwrite an alias method, the original method is "lost", as you removed the reference to that method. Zeriab's explanation seems concise enough. If you want to preserve the method, just alias the aliased method. But that makes the code hard to read.

Code:
def some_method  puts "Original Method"endsome_method=> Original Methodalias :some_method2 :some_methoddef some_method  puts "Aliased Method"endsome_method=> Aliased Methodalias :some_method3 :some_methoddef some_method  some_method2  some_method3endsome_method=> Original Method   Aliased Methoddef some_method2  puts "Overwrite Aliased Method"endsome_method=> Overwrite Aliased Method   Aliased Method#cannot call the original some_method anymore, no reference to it.alias :some_method4 :some_method3def some_method3  puts "Yet Another Aliased Method"  some_method4endsome_method=> Overwrite Aliased Method   Yet Another Aliased Method   Aliased Method
Edit: some error with the illustration of output. As I've said, it's not easy to read.
 
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
Ok, I think I have constructed an accurate representation of what I was trying to ask. - iI probably could have worded it much better initially.

class Alias_Test  def initialize    @data = ["Init Text", "Init Text2"]  end   def data    @data  end   alias :data_alias1 :data  def data    stuff = []    ["New Text","New Text2"].each do |item|      stuff << item    end    data_alias1 + stuff  end   # My Initial Attempt @ An Alias 'Overwrite'  # This produces a stack error due to looping infinately.  def data_alias1    my_stuff = []    ["My Text","My Text2"].each do |item|      my_stuff << item    end    data + my_stuff  end   def print_stuff    data.each do |d|      p d    end  end endstuff = Alias_Test.newstuff.print_stuff
Code:
class Alias_Test  # Your suggested method  # Does not print the data correctly.  # I am trying to remove the 'stuff' that alias 1 creates in place of 'my_stuff'  # This method keeps the old data from alias1, which I do not want  alias :my_data_alias :data_alias1  def data_alias1    my_stuff = []    ["My Text","My Text2"].each do |item|      my_stuff << item    end    my_data_alias + my_stuff  endend
When I print the data, it should show
=> "Init Text"
=> "Init Text2"
=> "My Text"
=> "My Text2"

I could achieve this by doing this...

  def data    my_stuff = []    ["My Text","My Text2"].each do |item|      my_stuff << item    end    @data + my_stuff  endAs you may notice, this overwrites the method. Therefore, completely voids my
whole attempt of trying to 'overwrite the aliased data'

I should state - this is not an important thing. I was simply wondering if it could be done without having to overwrite the original method.

Also, Thanks to the mod (i think Shaz) that moved this into a thread. It seemed to be getting a bit out of hand :/
 
Last edited by a moderator:

ShadowLurk

Tanoshii~
Veteran
Joined
Feb 14, 2014
Messages
226
Reaction score
53
Primarily Uses
The first case, It is looping because data_alias1 calls data, while the new data calls data_alias1. Just alias data like so:

alias :my_data_alias :data def data my_stuff = [] ["My Text","My Text2"].each do |item| my_stuff << item end data_alias1 + my_stuff endNow your previous data method can be called with my_data_alias. Although, I do not see any reason why do you want to do this....

Addendum: When you overwrite a method, you are not really overwriting the method code, you are merely making the method name points to your new method. That's why aliasing method works, as aliasing just means giving the method another name that points to that method.
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Now your previous data method can be called with my_data_alias. Although, I do not see any reason why do you want to do this....
This comes up often: you have happily added things like any other RM scripter, using proper aliasing to preserve previous changes done to the method.

But now you want to add something else, except one of the previous changes is incompatible!

You want to get rid of the thing entirely because it's messing things up.

Here is a simple use case

Base script: Returns 0Script1: Adds 1 to it. I have no requirements.Script2: Adds 2 to it. I have no requirements.Script3: Adds 3 to it. I have no requirements.Script4: I don't want to add that 2 to it! PLEASE REMOVE THAT 2! But I require script 3.You have a case where you don't want to add something to the value, but you can't do anything about it.Why does this happen? I don't know.

Is there a way to avoid it? Hard to say.

It stems from poor design for the most part, where one script prevents others from extending it.

The problem could be the base script, or the way you've written your own script.

This is why I tend to use excessive amounts of method definitions for every little thing, cause you never know when someone needs to kill off or alter one of your definitions cause they don't like it.
 
Last edited by a moderator:

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
Addendum: When you overwrite a method, you are not really overwriting the method code, you are merely making the method name points to your new method. That's why aliasing method works, as aliasing just means giving the method another name that points to that method.
Not quite. When you alias a method in Ruby, the interpreter makes a second copy of the method. Your alias then points to that copy.

What's the difference? The main one is that if somebody goes and changes the original method, your alias still works the way you wrote it. Sometimes I see scripts where people alias a method, then go back to using the original name. That defeats the purpose of aliasing.

Edit: when you overwrite a method, the original is gone. Technically, the code may exist somewhere on the heap until the garbage collector comes and takes care of it, but for practical purposes you've replaced the original method with the new one.

Edit2: Looking at your code Dekita - you're aliasing methods before their body has been defined yet, so there's nothing to make a copy of. First define the method, then alias it once you have a method body to copy. Conceptually, I don't see why you can't alias an alias - the first alias exists as it's own separately mutable entity. You'd just be making a copy of that instead of the original method. So you'd have original_method, alias_methodwithchanges, and alias_methodwithmorechanges (a copy of the method alias_methodwithchanges ready for whatever other changes you add)
 
Last edited by a moderator:

ShadowLurk

Tanoshii~
Veteran
Joined
Feb 14, 2014
Messages
226
Reaction score
53
Primarily Uses
Not quite. When you alias a method in Ruby, the interpreter makes a second copy of the method. Your alias then points to that copy.
Yeah, you are right. I forgot that detail so many times....

def something puts "something"end#in the script below italias :something_alias :somethingdef something something_alias puts "something else"endalias :something_for_change :somethingdef something something_for_changeend#in another scriptalias :something_alias_again :somethingdef something something_alias_again puts "something else again"end#in yet another scriptdef something_for_change something_alias puts "something changed"endand yeah, double aliasing might work.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
"Double aliasing" works because all it does is do the same operation, except it overwrites an existing method

You could just as well do this

class Test def print p 'fun' end def other p 'other' end # Perverse, but it works! alias :print :otherendTest.new.printIt is no different from manually copy-pasting the method and changing its name.Except it's dynamic.
 
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
Please forgive me, but I am not able to comprehend why I have to alias a method, when I want to completely overwrite it. - Or rather, why I would be calling the 'old alias data' at all. In this case - its poorly written garbage (imo) and shouldnt be kept.

Also, as Tsukihime pointed out...

Base script: Returns 0Script1: Adds 1 to it. I have no requirements.Script2: Adds 2 to it. I have no requirements.Script3: Adds 3 to it. I have no requirements.Script4: I don't want to add that 2 to it! PLEASE REMOVE THAT 2! But I require script 3.This was literally the whole thing that caused me to ask the question 'Can I overwrite the definition of an alias method - without changing the origonal method'... Basically, I wanted to achieve the following:

1 - Keep previously aliased methods

2 - Completely change the data defined within one alias,

3 - Not affect any aliases, to this method, that took place after the alias that I had 'overwritten'.

Also, @ Tsukihime..

Thats pretty clever... I never thought of aliasing something like that. I did however do this before (just testing crap - as you do)

Code:
class Test  def crap    p "I Have Crap!!"  end   alias :crap_II :crap  def crap    p "I Have More Crap!"    random_method  end   def random_method    crap_II    p "I Am Crap-Tastic!!"  endend
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I'd rather fix the other script if I intended for both to be used at the same time.


Otherwise just say it's not compatible and that they can only choose one or the other.


Or maybe that's just my easy way out to not fix poorly designed scripts.
 
Last edited by a moderator:

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
Please forgive me, but I am not able to comprehend why I have to alias a method, when I want to completely overwrite it. - Or rather, why I would be calling the 'old alias data' at all. In this case - its poorly written garbage (imo) and shouldnt be kept.
If that's what you want why don't you just delete the alias you don't want completely (as in remove the lines of code from the script editor so they no longer exist anywhere), and then overwrite the original method? Or alias the original method if you're set on doing the alias thing.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,204
First Language
Binary
Primarily Uses
RMMZ
Lol I did, days and days ago...

I mentioned (a while back) that this issue was no longer an issue, just a simple query. If it was easy to achieve, it could be useful for increasing compatibility between scripts. I ended up simply deleting the other code completely and just replacing it with my code. It was much easier.

If this could be done though, as I said, it 'could' be useful.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I don't think it would be useful; just messy and an attempt to work around bad code.
 
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
lol I guess thats true.

Maybe a more useful way would be if it was possible to completely delete an aliased method, without affecting the rest of the code... Hmmm... thats probably just going to have the same problems...
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
I don't think it would be useful; just messy and an attempt to work around bad code.
In a long term multi-developer environment I can see it making sense. 

Developer A writes some code that aliases some methods. People use the code. Life is Good.

Developer B comes along and needs to add some functionality to just one of those methods. He can't delete the original alias, as a lot of code depends on it being there working the way it does, and he doesn't want to go back and re-alias the original method, as he wants it to be clear to future developers where this code came from - plus he wants to be sure his code just replaces the alias and not the original method, in case somebody is calling that from somewhere. So he goes and aliases Developer A's aliased method. Life is still Good.
 

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