这里有很多事情要做,不是吗?
# 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