From: "Jesús Gabriel y Galán" Date: 2010-02-19T02:41:53+09:00 Subject: Re: Method prototypes like C++? // Compiling executables On Thu, Feb 18, 2010 at 6:28 PM, Claudio Freda wrote: > Rob Biedenharn wrote: >> Sure just do it!  Methods only need to exist when then are "called" >> and even then you can use the method_missing hook to define them on >> the fly if you need to. > > Wait, I explain my problem. > In C++ I would have been able to to this: > > void function(); > function(); > void function() { >  cout<<"The function has been executed" > } > > And it would still print "The function has been executed" > > In ruby instead if I do this: > > def method; end > > method > > def method >  print ('The method has been executed') > end > > it just prints me nothing; I think it has something to do with ruby's > dynamic definitions. > > Just how to achieve the same thing in ruby? (summoning a function that > is declared later in the code) What Rob wanted to say is that you don't need to declare a function (method). You just call it, and it has to have been defined before the call. But be aware that the code inside a method is not executed when it's parsed, only when the method is called, so you can have this: irb(main):001:0> def caller irb(main):002:1> method("a") irb(main):003:1> end => nil irb(main):004:0> def method(s) irb(main):005:1> puts s irb(main):006:1> end => nil irb(main):007:0> caller a You can have the definition of caller before the definition of method. What you can't do is call caller before defining method. Hope this clears up a bit, Jesus.