雄辩的Ruby:构建迭代器,通过不';事实上并不存在

雄辩的Ruby:构建迭代器,通过不';事实上并不存在,ruby,Ruby,我在读罗斯·奥尔森的《雄辩的红宝石》。在下面的代码(Loc 3264)之后,他说:“请注意,我们从来没有真正构建一个包含所有单词对的四元素数组:我们只是动态生成这些单词对。”我不明白这里发生了什么。那么什么是文字呢 class Document #Most of the class omitted... def each_word_pair word_array = words index = 0 while index &l

我在读罗斯·奥尔森的《雄辩的红宝石》。在下面的代码(Loc 3264)之后,他说:“请注意,我们从来没有真正构建一个包含所有单词对的四元素数组:我们只是动态生成这些单词对。”我不明白这里发生了什么。那么什么是文字呢

class Document
    #Most of the class omitted...

    def each_word_pair  
        word_array = words
        index = 0
        while index < (word_array.size-1)
            yield word_array[index], word_array[index+1]
            index += 1
        end
    end
end

doc = Document.new('Donuts', '?', 'I love donuts mmmm donuts')
doc.each_word_pair{|first, second| puts "#{first} #{second}"}
#=> I love
#=> love donuts
#=> donuts mmmm
#=> mmmm donuts
类文档
#这门课大部分被省略了。。。
定义每个单词对
单词数组=单词
索引=0
而索引<(单词数组大小-1)
产生单词数组[索引],单词数组[索引+1]
指数+=1
结束
结束
结束
doc=Document.new('Donuts','?','I love Donuts mmmm Donuts')
每个单词对{第一,第二}放置“{first}{second}”
#=>我爱你
#=>爱甜甜圈
#=>甜甜圈毫米
#=>毫米甜甜圈
那么什么是文字呢

class Document
    #Most of the class omitted...

    def each_word_pair  
        word_array = words
        index = 0
        while index < (word_array.size-1)
            yield word_array[index], word_array[index+1]
            index += 1
        end
    end
end

doc = Document.new('Donuts', '?', 'I love donuts mmmm donuts')
doc.each_word_pair{|first, second| puts "#{first} #{second}"}
#=> I love
#=> love donuts
#=> donuts mmmm
#=> mmmm donuts
words
是字符串
“我爱甜甜圈mmmm甜甜圈”
,而不是他所说的“所有单词对的四元素数组”

“注意,我们从来没有真正构建一个包含所有单词对的四元素数组:我们只是动态生成这些单词对。”我不明白这里发生了什么

他指的是数组
[[“I”,“love”],[“love”,“donuts”],[“donuts”,“mmmm”],[“mmmm”,“donuts”]]
每个单词对
方法中从来都不存在。这是因为它创建每个单词对,然后
将它们交给调用块。因此它生成第一对(
[“I”,“love”]
),
产生它,然后为下一个产生它。但是
每个单词对本身并不包含所有四对

例如,我们本可以采用类似的方法:

def word_pairs
  word_array = words
  word_pairs = []

  index = 0
  while index < (word_array.size-1)
    word_pairs << [word_array[index], word_array[index+1]]
    index += 1
  end

  word_pairs
end
def字对
单词数组=单词
单词对=[]
索引=0
而索引<(单词数组大小-1)
词对
那么什么是文字呢

class Document
    #Most of the class omitted...

    def each_word_pair  
        word_array = words
        index = 0
        while index < (word_array.size-1)
            yield word_array[index], word_array[index+1]
            index += 1
        end
    end
end

doc = Document.new('Donuts', '?', 'I love donuts mmmm donuts')
doc.each_word_pair{|first, second| puts "#{first} #{second}"}
#=> I love
#=> love donuts
#=> donuts mmmm
#=> mmmm donuts
words
是字符串
“我爱甜甜圈mmmm甜甜圈”
,而不是他所说的“所有单词对的四元素数组”

“注意,我们从来没有真正构建一个包含所有单词对的四元素数组:我们只是动态生成这些单词对。”我不明白这里发生了什么

他指的是数组
[[“I”,“love”],[“love”,“donuts”],[“donuts”,“mmmm”],[“mmmm”,“donuts”]]
每个单词对
方法中从来都不存在。这是因为它创建每个单词对,然后
将它们交给调用块。因此它生成第一对(
[“I”,“love”]
),
产生它,然后为下一个产生它。但是
每个单词对本身并不包含所有四对

例如,我们本可以采用类似的方法:

def word_pairs
  word_array = words
  word_pairs = []

  index = 0
  while index < (word_array.size-1)
    word_pairs << [word_array[index], word_array[index+1]]
    index += 1
  end

  word_pairs
end
def字对
单词数组=单词
单词对=[]
索引=0
而索引<(单词数组大小-1)

单词配对它将空格提供的句子拆分成数组,单词是数组,但执行此操作的代码可能在前面的一个示例中:-)它将空格提供的句子拆分成数组,单词是数组,但执行此操作的代码可能在前面的一个示例中:-)我理解你的解释他试图完成的,但当我运行代码时,它返回单个字符(而不是单个单词)。你如何获得完整的单词?@ltrainpr你提供的代码不完整,因为它实际上没有运行,所以我无法对它进行完全测试。但在某些情况下,
words
应该是
split
on
。我认为单词实际上应该包含一个单词数组,例如
words=“一个示例句子”。split
这样,您将得到预期的结果。此
每个单词对
方法的等价物不是
每个片段(2)
,而是
每个片段(2)
@toro2k哎呀,太尴尬了!谢谢你指出我的深夜错误
:)
。听了你的解释,我明白他想完成什么,但当我运行代码时,它会返回单个字符(而不是单个单词)。你如何获得完整的单词?@ltrainpr你提供的代码不完整,因为它实际上没有运行,所以我无法对它进行完全测试。但在某些情况下,
words
应该是
split
on
。我认为单词实际上应该包含一个单词数组,例如
words=“一个示例句子”。split
顺便说一下,你会得到预期的结果。这个
每个单词对
方法的等价物不是
每个片段(2)
,而是
每个片段(2)
@toro2k哇,太尴尬了!谢谢你指出我的深夜错误
:)