Mark B. wrote:
I would like to be able to call a function (or a class,
when I get more object-oriented) earlier in the program
file than where I define it. Such a “top-down” layout
seems natural to me. In C I could use a function
prototype for this.
The reason why you have to declare a function in C before you call it
meaning the code position) is fundementally different from why you have
define it in ruby before you call it (here before means the runtime).
Works just fine (because the code in foo that references bar executes
bar has been defined (even if it’s position in the code is earlier).
bar #=> 1
bar #=> 2
You can redefine methods and ruby always executes the version of the
that’s currently in existence.
bar # Now it works
You can define method a within method b. That will have the effect that
running method b, method a will be defined (although it wasn’t before).
You can also redefine methods that way:
def foo() 1 end
def bar() def foo() 2 end end
foo #=> 1
foo #=> 2
All this has the effect that you can’t forward declare methods like you
C. If you do this:
def foo() end
def foo() bar end
The call in line 2 will call the empty version of foo because that is
version currently in existence.