Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 呼叫';每个';在一个街区内的范围内做什么?_Ruby - Fatal编程技术网

Ruby 呼叫';每个';在一个街区内的范围内做什么?

Ruby 呼叫';每个';在一个街区内的范围内做什么?,ruby,Ruby,我应该定义一个方法substring,该方法将返回一个哈希,列出与已定义的字符串数组匹配的子字符串的出现次数。这是密码 dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"] def substrings(sentence, dictionary) substrings = Hash.new(0) wo

我应该定义一个方法
substring
,该方法将返回一个哈希,列出与已定义的字符串数组匹配的子字符串的出现次数。这是密码

dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"]

def substrings(sentence, dictionary)
  substrings = Hash.new(0)
  words = sentence.split(" ")
  words.each { |word|
    word = word.downcase
    characters = word.split("")
    length = characters.length
    (0...length).each { |start|
      substring = []
      (start...length).each { |stop|
        substring.push(characters[stop])
        if (dictionary.include?(substring.join))
          substrings[substring.join] += 1
        end
      }
    }
  }
  puts substrings
end

substrings("Howdy partner, sit down! How's it going?", dictionary)
我对这段代码感兴趣

(0...length).each { |start|
(0...length).each { |start|

我想知道调用每个方法的是什么。我没有看到显式列出要调用的
每个方法的数组。列出了一个范围,但它如何知道该范围所指的数组?它只是在当前所在的块上调用它吗?

长度定义为:

length = characters.length
characters = word.split("")
words = sentence.split(" ")
字符
定义为:

length = characters.length
characters = word.split("")
words = sentence.split(" ")
word
是对
words
进行迭代的块变量,其定义如下:

length = characters.length
characters = word.split("")
words = sentence.split(" ")

换句话说,
(0…长度)。每个
迭代
单词
中的每个字符索引,这是通过将
句子
拆分为一个空格生成的。

长度
定义为:

length = characters.length
characters = word.split("")
words = sentence.split(" ")
字符
定义为:

length = characters.length
characters = word.split("")
words = sentence.split(" ")
word
是对
words
进行迭代的块变量,其定义如下:

length = characters.length
characters = word.split("")
words = sentence.split(" ")
换句话说,
(0…长度)。每个
迭代
单词
中字符的每个索引,该索引是通过将
句子
拆分一个空格生成的

我对这段代码感兴趣

(0...length).each { |start|
(0...length).each { |start|
我想知道调用每个
方法的是什么

它被称为上

我没有看到显式列出要调用的
每个方法的数组

为什么每个
都需要
数组
?有很多类实现了
每个
:,,,,,,这些只是核心库中的类。标准库中甚至还有更多,例如,和、、、和、中、中、中、中、中,可能还有更多。当然,在更广泛的Ruby生态系统中可能还有数百个其他的,例如,RubyonRails中有14个
的实现

列出了一个范围,但它如何知道该范围所指的数组

没有。
范围
不是指数组。它指的是一个范围

它只是在当前所在的街区调用它吗

不能在块上调用方法。您只能对对象调用方法,而块在Ruby中不是对象。(你可以具体化一个块,但它会变成一个普通的
Proc
对象。而且
Proc
不会对
每个
做出响应,所以你也不能调用
每个
。)

我对这段代码感兴趣

(0...length).each { |start|
(0...length).each { |start|
我想知道调用每个
方法的是什么

它被称为上

我没有看到显式列出要调用的
每个方法的数组

为什么每个
都需要
数组
?有很多类实现了
每个
:,,,,,,这些只是核心库中的类。标准库中甚至还有更多,例如,和、、、和、中、中、中、中、中,可能还有更多。当然,在更广泛的Ruby生态系统中可能还有数百个其他的,例如,RubyonRails中有14个
的实现

列出了一个范围,但它如何知道该范围所指的数组

没有。
范围
不是指数组。它指的是一个范围

它只是在当前所在的街区调用它吗


不能在块上调用方法。您只能对对象调用方法,而块在Ruby中不是对象。(你可以具体化一个块,但它会变成一个普通的
Proc
对象。而且
Proc
不会对
每个
做出响应,所以你也不能调用
每个

谢谢!正是我需要的解释@MasonTowne另一种表达
(0…长度)的方式。每个
都是
长度。时间
谢谢!正是我需要的解释@马斯顿拥有另一种表达
(0…长度)的方式。每一种
都是
长度。泰晤士报
这样看<代码>e=(3..9)。每个#=>#
。Ruby然后使用该方法生成
e
的元素,并将其传递给块,将其值分配给块变量
start
start=e.next#=>3;start=e.next#=>4
等等。这样看<代码>e=(3..9)。每个#=>#
。Ruby然后使用该方法生成
e
的元素,并将其传递给块,将其值分配给块变量
start
start=e.next#=>3;开始=e.next#=>4
等等。