From: Robert Klemme Date: 2004-07-31T04:36:51+09:00 Subject: Re: Forward references? "Lloyd Zusman" schrieb im Newsbeitrag news:d62dd0tu.fsf@asfast.com... > 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 > > Am I totally out of luck, or is there some kind of ruby trick I can > perform which will give me this capability? Just define them duplicately, the first time with an empty body: # "prototypes" def foo;end def bar(name)end # main stuff x = foo() y = bar() puts x puts y exit 0 # "functions" def foo # foodoo end def bar(name) # bardo end Regards robert