Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试在一个文件中编写一个 Ruby 脚本。
我想知道是否可以在开始时编写“main”函数,并在其后定义 main 使用的其他函数。换句话说,我想调用一个尚未定义的函数,以便它们不依赖于定义顺序。仅更改顺序是不可能的,因为它会给出“未定义的方法”错误。在 C/C++ 中,我们使用前向声明......在 Ruby 中是否有类似的东西或其他解决方案?
您只需要在main函数运行时定义调用的函数,而不是在定义函数时。因此,最简单的解决方案是main在脚本的开头编写函数,但在结尾调用它。
main
def main foo(42) bar(24) end # definitions of foo and bar main