#!/usr/bin/env ruby
=begin
**********************************************************************
This is a comment block, something you write for the benefit of
human readers (including yourself). The interpreter ignores it.
There is no need for a '#' at the start of every line.
**********************************************************************
=end
組織你的代碼
Ruby讀到什麼就處理什麼.沒有編譯處理;如果有什麼還沒讀到,就被簡單地認為未定義.
# this results in an "undefined method" error:
print successor(3),"\n"
def successor(x)
x + 1
end
# Conversion of fahrenheit to celsius, broken
# down into two steps.
def f_to_c(f)
scale(f - 32.0) # This is a forward reference, but it's okay.
end
def scale(x)
x * 5.0 / 9.0
end
printf "%.1f is a comfortable temperature.\n", f_to_c(72.3)
#!/usr/bin/env ruby
def main
# Express the top level logic here...
end
# ... put support code here, organized as you see fit ...
main # ... and start execution here.
Ruby也提供了將複雜程式分割為可讀,可重用,邏輯相關的大塊的工具.我們已看到用 include 來訪問模組.你將發現 load 和 require 也很有用.load的作用類似於檔案的複製加粘貼(和C的#include處理器指令相似).require更複雜,僅在需要時才載入,而且最多載入一次.load和require還有其它一些區別;在語言手冊,FAQ中可找到更多資訊.