這裏有很多事情要做,不是嗎?
# Add the lib/ directory to the require search path
$:.unshift(File.dirname(__FILE__)+'/../lib/')
# Load in the statsample file which presumably defines Statsample
# This file may require others as necessary
require 'statsample'
# This makes a call to Statsample::Analysis.store with a block provided
Statsample::Analysis.store(Statsample::Dataset) do
samples=1000
# This calls something to do with Statsample::Vector but the implementation
# would define exactly what's going on with that block. Not clear from here.
a = Statsample::Vector.new_scale(samples) {r=rand(5); r==4 ? nil: r}
b = Statsample::Vector.new_scale(samples) {r=rand(5); r==4 ? nil: r}
# Converts a simple hash with 'a' and 'b' keys to a dataset using some kind
# of Hash method that's been added by the statsample implementation.
ds = { 'a'=>a,'b'=>b }.to_dataset
# Calls a method that sets the summary to the hash
summary(ds)
end
# __FILE__ => Path to this source file
# $0 => Name of script from command line
# If the name of this file is the name of the command...
if __FILE__==$0
# ..run batch.
Statsample::Analysis.run_batch
end
一般來說,您必須深入實施以瞭解如何使用這些塊。存在用於Ruby的定義塊中的兩個基本格式:
some_method do
...
end
some_method { ... }
這兩個都是等效但大括號版本通常用於簡潔起見,因爲它很容易摺疊成一個單一的線。
塊可能有點令人困惑,因爲它們只是在傳遞給它們的方法意志上執行的代碼片段。它們可能永遠不會被執行,或者可能會被執行一次或多次。該塊也可以在不同的上下文中執行。您需要通過閱讀文檔或實施分析或其他示例,仔細注意方法在塊中要求的內容。
通常叫做Statsample::Vector
根據您在這裏張貼什麼lib/statsample/vector.rb
定義,但它也可以在lib/statsample.rb
取決於作者的組織策略進行定義。類或模塊名稱和文件名之間的相關性僅由慣例驅動,而不是任何特定的技術要求。
打我吧 - 我只有2/3完成:)很好的解釋,雖然 – Kelly 2011-05-11 19:28:17