Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
  • 0
Sign in to follow this  
Heretic86

XP Script - Cant create an Alias for a Def?

Question

Does Ruby in RMXP have a limit on the number of Aliases that are allowed?

 

Trying to write a new def, and keeps spitting NameErrors at me, bla bla Undefined Method, the method I just defined.

 

alias test_test test

def test

print "this is a test"

end

 

---

 

Edit, nevermind, I feel dumb now.

Edited by Heretic86

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Was trying to create an alias for a def not defined yet.

 

Half the script I was working on was redeclaring the defs, but putting alias before def (which was a redef), so slightly confusing. The script contained a bunch of self references.

 

alias test already_defined_def

def test

print "Hello"

test

return

end

Edited by Heretic86

Share this post


Link to post
Share on other sites
  • 0

The above is still wrong...

 

You are missing the original method name in the alias declaration, and the method is recursive, which Ruby does not allow.

Share this post


Link to post
Share on other sites
  • 0

Yeah, I figured that part out. The def was confusingly written and I hadnt seen it in other parts of the official script, so I wasnt aware that it had been previously defined. Given that was what I saw in the addon script, I thought the Alias could be delared before the Definition.

 

So yes, in correct terms it would be:

 

def foo # Original Unmodified Code
 print "foo"
end

(another page)

alias bar foo
def foo
 do something else other than print foo
 bar # Reference to alias inside Def
 end
end

Share this post


Link to post
Share on other sites
  • 0

Ok, Im totally lost again. I opened my Mouth and my Brain fell out!

 

class lorem_ipsum
 def foo
   print "foo"
 end
 alias foo bar

 def foo
   print "bar"
 end

 def initialize
   foo #prints "Bar"
   bar #prints "Foo"
 end
end

 

WTF?

Share this post


Link to post
Share on other sites
  • 0

First off, that code shouldn't work. The correct syntax for alias is:

 

alias new_method_name original_method_name

 

all Alias does, is (essentially) make a "copy" of the original method definition, and rename it to the new method name provided.

 

Since ruby provides open classes/methods, you can redefine the old method (AFTER the alias), which that definition will not override the aliased method (as it contains a reference to the definition that was in place at the time of calling alias)

 

:/ I hope that made a little sense. It's kind of hard to explain....

 

With that snippet you posted:

 

1. `foo` is defined

def foo -> [print "foo"]

 

2. `foo` is aliased to `bar` (should be `alias bar foo`)

def foo -> [print "foo"]

def bar -> [print "foo"]

 

3. `foo` is redefined, `bar` still references original `foo` definition

def foo -> [print "bar"]

def bar -> [print "foo"]

 

Then, when the class is instantiated, the constructor method is called (which calls foo, then bar) and `foo` prints "bar" and `bar` print "foo"

 

This is how you usually see custom rpg maker xp scripts use alias, the constructor method is aliased (to keep the original method definition), then the constructor (or any other method really, though initialize is most common imo) is redefined with new functionality and a call to the old method:

# default script
class Foo
 def initialize
#do stuff
 end
end

#custom script
class Foo
 alias new_initialize initialize
 def initialize
new_initialize # original constructor is called
#do new stuff
 end
end

 

EDIT: Is there a specific reason to why you are using alias? Are you trying to just learn how it works or are you trying to accomplish a specific task that necessitates an alias?

Share this post


Link to post
Share on other sites
  • 0

That makes a lot of sense! Now, to see if I understood that correctly, the Alias is of the First Definition of Foo, not the second, so calling to Bar thus prints Foo because it is now the first definition of Foo that has been aliased with Bar, and running Foo runs the 2nd def of FOo, which prints Bar, not Foo.

 

The reason for it is Im still trying to modify that [explicitive deleted colorful metaphor] ATB script, and it uses a TON of aliases. Just to keep it short and simple, I am trying to make a change to it, and since it is (for all essencial purposes) uncommented, understand it by just understanding the code. Nearly everything in this script was using aliases like this:

 


class bla
 attr_accessor   :punk # some comment

 alias jerk punk
 def punk
   print "Something"
   jerk
 end
end

 

In that quib of code, it appeared to me that alias was being used before running the def, so I didnt get that the def had already been declared previously in unmodified code. I thought that was being done to self reference inside the def, which appears quite a bit in any of the UPDATE code. What it looked like afterwards was that the def was being completely redefined, and the old version dumped, but now that my understanding is more solid, I think I can follow the code without getting screwed up by referring back to the alias calls to the original definition, not the updated definition.

 

Thank you very much for helping to improve my understanding of Ruby! Im not a total idiot, some parts are missing! :P

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...