目前,我正在阅读ZedA.Shaw的“艰难地学习Ruby”一书,我很难理解练习48。我不明白的是这段测试代码:
class LexiconTests < Test::Unit::TestCase
Pair = Lexicon::Pair
@@lexicon = Lexicon.new()
def test_directions()
assert_equal([Pair.new(:direction, 'north')], @@lexicon.scan("north"))
result = @@lexicon.scan("north south east")
assert_equal(result, [Pair.new(:direction, 'north'),
Pair.new(:direction, 'south'),
Pair.new(:direction, 'east')])
end为什么我们需要使用结束语=词库::结对?这段代码创建了什么?
发布于 2020-03-29 01:22:27
正如@steens拉格所指出的,这是一种任务,就像一条捷径。在某些语言中,这种想法被称为别名。
Pair = Lexicon::Pair这允许您在整个类的其余部分中将Lexicon::Pair称为Pair。生成的代码不那么冗长。
https://stackoverflow.com/questions/60903411
复制相似问题