Bigace360 38 Report post Posted June 7, 2012 (edited) Okay so I've seen multiple ways to alias and I want to know, whats the difference between each one? alias ace_initialize initialize alias :ace_initialize :initialize alias ace_initialize initialize unless $@ alias_method :ace_initialize, :initialize Also why do people add block when the alias: alias :ace_initialize :initialize def initialize(*args, &block) ace_initialize(*args, &block) stuff more_stuff end I know what the argument is for, but what is the block for? Edited June 7, 2012 by bigace Share this post Link to post Share on other sites
ForeverZer0 44 Report post Posted June 7, 2012 For the first question regarding naming, there is no difference. I have never personally used the "unless $@", which is a builtin global that contains the backtrace of the last exception. Its just used as a way to check that the attempt to alias does not cause an exception, since if it did, the value of $@ would not equal nil, and therefore the evaluation would return true, preventing the alias name being given. For passing a block to a method call, it provides a way to pass a block as argument to the method, basically a unnamed method. Same difference as this: array = ['1', '004', '4gfg', '5656'] array.sort! # Without block array.sort! {|a, b| a.size <=> b.size } # With block Share this post Link to post Share on other sites
Bigace360 38 Report post Posted June 7, 2012 For the first question regarding naming, there is no difference. I have never personally used the "unless $@", which is a builtin global that contains the backtrace of the last exception. Its just used as a way to check that the attempt to alias does not cause an exception, since if it did, the value of $@ would not equal nil, and therefore the evaluation would return true, preventing the alias name being given. For passing a block to a method call, it provides a way to pass a block as argument to the method, basically a unnamed method. Same difference as this: array = ['1', '004', '4gfg', '5656'] array.sort! # Without block array.sort! {|a, b| a.size <=> b.size } # With block Okay so it doesnt matter which alias I use as they all give the same results. Ok cool. I guess that all makes since. Share this post Link to post Share on other sites
kellessdee 48 Report post Posted June 7, 2012 To add to foreverZer0's response: the difference between: alias :new_func :func alias_method :new_func, :func is minimal, but there is a small bit: alias is a keyword, and alias_method is a method call. Note the comma in "alias_method" alias_method(:new_func, :func) Next, the "alias" can only take "identifiers" or "symbols" alias new_func func #identifiers alias :new_func :func #symbols Symbols are an object, and are used to represent identifiers internally. Also commonly used as symbolic constants--a symbol is basically an immutable string, in which only ONE instance of the object exists in memory (per value). The "alias_method" method, exists in the module class (can only be called from within a class/module definition) and can only take "symbols" or "strings" alias_method(:new_func, :func) alias_method 'new_func', 'func' note: calling a method in Ruby does NOT require you include parentheses. Finally, alias behaves differently depending on the scope/context of the call. alias_method always acts as a call to the Class/Module objects "alias_method" method--which is another thing: alias_method can be redefined, alias cannot. Also, alias_method cannot be called in the global scope, whereas alias can. Otherwise, generally, there isn't much of a difference--at class/module scope, alias/alias_method behave the same. also (this part is just my preference) I generally use: unless method_defined?(:new_func) alias :new_func :func end Which works the same as the "unless $@" trick. However, what you SHOULD know about this, is that it is ONLY necessary (I think) for when you start alias/redefining internal (Sprite, Window, Bitmap) classes, and it is to prevent "F12 reset errors." The problem is (from what I can tell) when you reset an RPG Maker game with F12, it will reload all user scripts (redefining all classes) even though the interpreter retains all definitions, etc. It, however, doesn't *seem* to redefine any of the internal classes, and when it hits the alias for an internal class the second time, it will re-alias the new method with the new method, which makes the new method reference itself. *exhales* Then, when your aliased method calls itself, RPG Maker cries about "Stack Level Too Deep" because the self-referential method call becomes an endless loop and RPG Maker doesn't allow recursion anyways. But yea, those are mostly edge cases that one might not run into, but it's good to be aware of their existence; and mostly what I have learned from my own experience. I thought I would add it to what FZer0 said, in case you were curious. Share this post Link to post Share on other sites
ForeverZer0 44 Report post Posted June 7, 2012 (edited) The F12 error is a major oversight by Enterbrain. They should have re-initialized Ruby within a new scope, but instead it simply re-loads the scripts into the same scope. The common fix instead of all the checks to see if the method is defined is to just post this little snippet in the top slot of the editor: if $game_exists system('Game.exe') exit end $game_exists = true This will simply start a new program and exit the current one, which prevents the possibility of loading scripts twice. Edited June 7, 2012 by ForeverZer0 1 kellessdee reacted to this Share this post Link to post Share on other sites
kellessdee 48 Report post Posted June 8, 2012 That's an awesome little snippet. I never would have thought to forcibly restart the program--[that would save all those extra "unless $@"s or "unless method_defined?(...)"s. ]<---EDIT: Which is what you said LOL; I just realized I was repeating you. Share this post Link to post Share on other sites
Bigace360 38 Report post Posted June 8, 2012 Zeriab wrote something like that 6 years ago: unless $f12_cleaner_F3XXEFA1.nil? # Opens the game executable in a new thread Thread.new{system ("Game")} # "Game" = Name of the game executable # Exits this thread exit end # Modifying this global variable might cause this snippet to stop working properly. # Some garbage have been added to lessen the chance of it already being used $f12_cleaner_F3XXEFA1 = true Share this post Link to post Share on other sites
ForeverZer0 44 Report post Posted June 8, 2012 As I said, its a common fix. Share this post Link to post Share on other sites
Bigace360 38 Report post Posted June 8, 2012 Oh I missed that Share this post Link to post Share on other sites
kellessdee 48 Report post Posted June 9, 2012 oh sh-- I've been exposed. I'm actually *relatively* new to the RPG Maker XP+ scene. This is literally the first time I've ever seen this bit of code. There's always "new" stuff popping up in the RM World for me, it's kind of neat, even when it's older stuff I can see why it isn't used in release code very much though; Imagine a project with one of these blocks for every custom script lol. It would be funny if by some fluke, two scripters used the same name for their global. Infinite RPG Maker-restart loop.. *runs off to try it* Nonetheless, I wasn't exaggerating when I said I thought that snippet was really awesome. It's just one of those simple solutions that you don't think of; then when you see it all you can think is "damn that's a good idea!" Or is it just me that gets those? EDIT: lol, apparently RPG Maker restart loops just make everything go to hell. Share this post Link to post Share on other sites
ForeverZer0 44 Report post Posted June 9, 2012 For some real fun, O suggest adding this snippet in the top slot of the editor: 10.times { Thread.new { system('Game.exe') } } Its a test of speed to see if you can close the windows in time before your RAM maxes out from too many open windows. Share this post Link to post Share on other sites
Bigace360 38 Report post Posted June 9, 2012 You would kill my computer with that, still curious though Share this post Link to post Share on other sites
ForeverZer0 44 Report post Posted June 9, 2012 I accidentally did it before (not the 10 times thing, but a single window). I was trying to attach a process to the STDOUT, but had some code in the wrong place, and before you know it, I had a very nice loop of one process starting another. I use Process Lasso, and luckily it has a feature to disallow a process, so basically just set that real quick and it killed the current ones while preventing a new ones from getting going. Other than that, yeah, you better be quick. World record quick. Share this post Link to post Share on other sites
kellessdee 48 Report post Posted June 10, 2012 I had a very nice loop of one process starting another. Who needs memory leaks when you have process leaks? Lol, at least it didn't hide itself in the code, to jump out when you least expect it. Or by the time you forgot what you wrote. This sounds like a challenge. How fast is world record fast? Edit: Bravo, I failed that one. That was pretty cool, I've never actually witnessed my PC running out of memory before, nice experience. It was also the first time I saw windows 7 throw up a dialog asking to "End Process" I had to restart, there wasn't enough memory to get to that button. Or, I wasn't patient enough. That was like a self-inflicted zip bomb Share this post Link to post Share on other sites
ForeverZer0 44 Report post Posted June 10, 2012 Try it an find out. Share this post Link to post Share on other sites
kellessdee 48 Report post Posted June 10, 2012 lol you posted basically right when I edited my post--if you want to know the results look at my other post for the "Edit:" Share this post Link to post Share on other sites