Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> aliasing a method question
Adrien.
post Aug 27 2011, 06:36 PM
Post #1


Bet Mapper
Group Icon

Group: Banned
Posts: 1,632
Type: Mapper
RM Skill: Advanced




I understand the conecept as such:

CODE
class SomeClass

   def isOn?
      @on = true
   end

end

class SomeOtherClass < SomeClass

   alias :isOn_New? :isOn?
   def isOn?
      isOn_New?
      @isOff = false
   end

end


So I understand this. but what if I wanted to say I don't like the method isOn? and I want to re-write it entrily and make what ever application uses that method use my new method?

would it look like:

CODE
class SomeOtherClass < SomeClass

   alias :isOn_New? :isOn?
   def isOn?
        @isOff = false
   end

end


am I understanding this correctly?


__________________________



Games I am working on | Each image is a link to the game
Go to the top of the page
 
+Quote Post
   
Moonpearl
post Aug 28 2011, 12:45 AM
Post #2


Level 5
Group Icon

Group: Member
Posts: 73
Type: Developer
RM Skill: Advanced




Actually, aliasing a method is a technique which allows to re-use a method that was already written without rewriting everything. It is especially useful when you write several scripts which mess with the same methods, but do not necessarily want to use them together. Aliasing methods thus allows to re-use what has been previously written, be it in another custom script or in the standard engine.

Technically, what you wrote does replace the existing method, but saves the old one while you don't intend to use it anymore. So, if you want to fully replace an existing method, don't use aliasing at all, it's as simple as that. The new method will simply overwrite the previous one.

Besides, if you're talking about re-using a method from a superclass (the way you do in your example), method aliasing is not appropriate, since you can use the keyword super instead (which automatically calls the method with the same name from the superclass).


__________________________





Go to the top of the page
 
+Quote Post
   
Zeriab
post Aug 28 2011, 02:49 AM
Post #3


Level 12
Group Icon

Group: Revolutionary
Posts: 196
Type: Event Designer
RM Skill: Skilled




When you alias a method you give that method another alias. I.e. another name.
CODE
class SomeClass
   def isOn?
      @on = true
   end
   alias :isOn_New? :isOn?
end

foo = SomeClass.new
foo.isOn?
foo.isOn_New?


You can use both foo.isOn? and foo.isOn_New?
It is the same method you are calling.

The interesting part about the aliases is that if you overwrite a method any aliases will keep referring to the old method rather than the new one.
Moonpearl mentions the most common usage of aliases, but it can be useful without overwrites. Just look at the standard File class which has both exist? and exists? where writing that method twice would just be redundant. You can also try looking through the Array class and see how many methods could just be aliases of other methods.

*hugs*


__________________________
Go to the top of the page
 
+Quote Post
   
Moonpearl
post Aug 28 2011, 07:35 AM
Post #4


Level 5
Group Icon

Group: Member
Posts: 73
Type: Developer
RM Skill: Advanced




As Zeriab says, what you do when aliasing a method is giving it a second name. So, when you want to give method A additional functonalities, what you do is label it B, then rewrite A with a call to B inside. This way whenever A is called, your new method responds instead of the old one - the old one is then called by the new one. So if you want to override method A, better rewrite it without further consideration.


__________________________





Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 19th May 2013 - 11:08 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker