Functions need declaring before use


(Daniel) #1

I’ve noticed that functions require that they be declared before they can
be used. This came as somewhat as a blow as I orgaznie my code by putting
my main code first, and function listing at the end. Is this set in stone?
Plans to change? Reasoning?

Daniel


(Niklas Frykholm) #2

[Daniel]:

I’ve noticed that functions require that they be declared before they can
be used. This came as somewhat as a blow as I orgaznie my code by putting
my main code first, and function listing at the end. Is this set in stone?
Plans to change? Reasoning?

This is not likely to change. Ruby processes the file from beginning
to end and evaluates calls and definitions as they are encountered.
Ruby is dynamic, everything can be redefined and any other evaluation
order would likely lead to confusion, incompability and other
problems, in situations such as:

def a
	puts "hi"
end

a()

def a
	puts "bye"
end

a()

If you want to keep your style, I suggest you put your main code in
a main function and call it at the end of the program.

def main
	...
end

... other functions ...

main()

// Niklas


(Yohanes Santoso) #3

Daniel lists@debonair.net writes:

I’ve noticed that functions require that they be declared before they can
be used. This came as somewhat as a blow as I orgaznie my code by putting
my main code first, and function listing at the end. Is this set in stone?
Plans to change? Reasoning?

Daniel

Is this what you mean?

irb(main):001:0> def b
irb(main):002:1> a
irb(main):003:1> end
nil
irb(main):004:0> def a
irb(main):005:1> “aa"
irb(main):006:1> end
nil
irb(main):007:0> b
"aa”

YS.


(Pit) #4

[Daniel]:

I’ve noticed that functions require that they be declared before
they can be used. This came as somewhat as a blow as I orgaznie my
code by putting my main code first, and function listing at the end.
Is this set in stone? Plans to change? Reasoning?

If you want to keep your style, I suggest you put your main code
in a main function and call it at the end of the program.

def main

end

… other functions …

main()

You can also use an END block:

,----[ script ]

END {

main function

}

other functions

`----

Regards,
Pit

···

On 1 Jul 2002, at 17:09, Niklas Frykholm wrote:


(Daniel) #5

Thanks for these suggestions guys. Niklas, your explanation makes a lot of
sense. I’m still new to Ruby, but I see what you mean. Thanks for that
also.

Daniel

···

On Mon, 1 Jul 2002, Pit Capitain wrote:

On 1 Jul 2002, at 17:09, Niklas Frykholm wrote:

[Daniel]:

I’ve noticed that functions require that they be declared before
they can be used. This came as somewhat as a blow as I orgaznie my
code by putting my main code first, and function listing at the end.
Is this set in stone? Plans to change? Reasoning?

If you want to keep your style, I suggest you put your main code
in a main function and call it at the end of the program.

def main

end

… other functions …

main()

You can also use an END block:

,----[ script ]

END {

main function

}

other functions

`----

Regards,
Pit


Newlan’s Truism:
An “acceptable” level of unemployment means that the government
economist to whom it is acceptable still has a job.