简体   繁体   English

Ruby on Rails新手的代码演练

[英]Code Walkthrough for a Ruby on Rails Newbie

I am trying to understand how to use a ruby on rails library. 我试图了解如何在Rails库中使用红宝石。 I am new and can't even understand the most basic example. 我是新手,甚至无法理解最基本的示例。 The library I am trying to use is called statsample. 我尝试使用的库称为statsample。 Can someone help walk me through what is going in this code snippet: 有人可以帮助我逐步介绍一下此代码片段中的内容:

$:.unshift(File.dirname(__FILE__)+'/../lib/')
require 'statsample'

    Statsample::Analysis.store(Statsample::Dataset) do
      samples=1000
      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}

      ds={'a'=>a,'b'=>b}.to_dataset
      summary(ds)
    end

    if __FILE__==$0
      Statsample::Analysis.run_batch
    end

There's a lot going on here, isn't there? 这里有很多事情,不是吗?

# 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

Generally you have to dig down into the implementation to see how those blocks are used. 通常,您必须深入研究实现以查看如何使用这些块。 There are two basic formats for defining a block in Ruby: 在Ruby中定义块的基本格式有两种:

some_method do
  ...
end

some_method { ... }

These two are equivalent but the curly-brace version is often used for the sake of brevity, as it easily collapses into a single line. 这两个是等效的,但是为了简洁起见,通常使用花括号版本,因为它很容易折叠成一行。

Blocks can be a bit confusing because they are simply bits of code that are executed at the will of the method they are passed to. 块可能会有些混乱,因为它们只是代码的一部分,它们随它们所传递的方法的意愿而执行。 They may never be actually executed, or might be executed once, or many times. 它们可能永远不会实际执行,或者可能执行一次或多次。 The block can also be executed in different contexts. 该块也可以在不同的上下文中执行。 You need to pay careful attention to what the method is asking for in a block either by reading the documentation, or analysis of the implementation or other examples. 您需要通过阅读文档,对实现的分析或其他示例来仔细注意该方法在一个块中的要求。

Generally something called Statsample::Vector is defined in lib/statsample/vector.rb according to what you've posted here, but it could also be defined in lib/statsample.rb depending on the author's organizational strategy. 通常,根据您在此处发布的内容,在lib/statsample/vector.rb定义了称为Statsample::Vector内容,但也可以根据作者的组织策略在lib/statsample.rb定义它。 The correlation between class or module name and filename is only driven by convention, not any particular technical requirement. 类或模块名称与文件名之间的相关性仅受约定驱动,而不受任何特定的技术要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM