From: Mark Sparshatt Date: 2004-07-30T23:45:55+09:00 Subject: Re: Forward references? Lloyd Zusman wrote: >Is there a way to define forward references to functions? Due to my own >personal eccentricities, I like to have the main body of a program >appear at the top of its source file, and the functions it uses at the >end. > >By "define forward reference", I mean something analogous to the >following C construct: > > static char* foo(); /* forward reference */ > static char* bar(); /* forward reference */ > > main () { > printf("%s\n", foo()); > printf("%s\n", bar()); > exit(0); > } > > char* foo() { > return ("foo"); > } > > char* bar() { > return ("bar"); > } > >If I could make use of forward references in ruby, the above program >could look something like this: > > # somehow, define a forward reference to function foo() > > # somehow, define a forward reference to function bar() > > # program starts here > > x = foo() > y = bar() > puts x > puts y > exit(0) > > # real function definitions ... > > def foo > return "foo" > end > > def bar > return "bar" > end > > > Since Ruby doesn't check whether the functions exist until the code that calls them is actually run, one way to do this would be def main puts foo puts bar exit(0) end def foo return "foo" end def bar return "bar" end main -- Mark Sparshatt