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
等等。