From: "Einar Magnús Boson" Date: 2008-12-14T02:33:55+09:00 Subject: Re: Function def at end of scripts On 13.12.2008, at 09:11 , David A. Black wrote: > This message is in MIME format. The first part should be readable > text, > while the remaining parts are likely unreadable without MIME-aware > tools. > Hi -- > > On Sat, 13 Dec 2008, Einar Magnús Boson wrote: > >> >> On 13.12.2008, at 00:02 , ara.t.howard wrote: >> >>> On Dec 12, 2008, at 10:06 AM, No Where wrote: >>>> Hi, >>>> I am refactoring an ugly script. I would like it contained all in >>>> one >>>> file so I was moving code to functions/classes and put them at >>>> the end >>>> of the script for legibility. This didn't work. They are only >>>> available >>>> if I put them at the beginning of the script before I need them. Is >>>> there a way to declare them at the beginning so I can put them >>>> all at >>>> the end of the file? >>>> Thanks! >>>> Eric >>>> -- >>>> Posted via http://www.ruby-forum.com/. >>> stuff >>> BEGIN { >>> def stuff >>> end >>> } >>> a @ http://codeforpeople.com/ >>> -- >>> we can deny everything, except that we have the possibility of >>> being better. simply reflect on that. >>> h.h. the 14th dalai lama >> >> >> #!/usr/bin/env ruby -wKU >> END { >> puts "http://www.java2s.com/Code/Ruby/Statement/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm >> " } >> >> stuff >> >> BEGIN { >> def stuff >> puts "who would've guessed?" >> end >> } >> # >> who would've guessed? >> # >> http://www.java2s.com/Code/Ruby/Statement/ThecodeinablocklabeledwiththekeywordBEGINisrunautomaticallywhenaRubyprogramisloaded.htm >> >> This is cool. Thanks! > > It can serve a purpose, but please have mercy on those of us who like > our Ruby in order :-) It's really not that hard to get used to the > principle that the code (including method definitions) is executed as > it's encountered, and then you don't have to think about whether or > not to flip it. > > > David > > -- > Rails training from David A. Black and Ruby Power and Light: > INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL > See http://www.rubypal.com for details > Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) I actually have a hard time envisioning a scenario where I would ever use this. Still good to know of. Looking into it made me find some other weird stuff I had never come across before though, like the flip- flop operator that took me a while to figure out properly. That is also something I don't see myself using though, it has a bit too complicated semantics for my taste and feels like obfuscation just to avoid an extra line. einarmagnus