0

在完成 Hartl 的 Rails 教程的前 4 章后,我开始阅读 Ruby Koans。我被困在 about_constants.rb 的开头

公案旨在展示常量的层次结构。显然我在这里遗漏了一些简单的东西(这是我完成的第一次编程学习),但我似乎无法找出我哪里出错了。非常感谢任何见解,谢谢!

这是我在下面尝试的代码:

Macintosh:~ rails$ irb
2.0.0p247 :001 > C = "top level"
 => "top level" 
2.0.0p247 :002 > class AboutConstants
2.0.0p247 :003?>   C = "nested"
2.0.0p247 :004?>   end
 => "nested" 
2.0.0p247 :005 > C
 => "top level" # WRONG - correct response should be "nested"
2.0.0p247 :006 > ::C
 => "top level" # CORRECT

作为参考,我正在研究的 about_constants.rb 的顶部:

require File.expand_path(File.dirname(__FILE__) + '/neo')

C = "top level"

class AboutConstants < Neo::Koan

  C = "nested"

  def test_nested_constants_may_also_be_referenced_with_relative_paths
    assert_equal __, C
  end

  def test_top_level_constants_are_referenced_by_double_colons
    assert_equal __, ::C
  end

  def test_nested_constants_are_referenced_by_their_complete_path
    assert_equal __, AboutConstants::C
    assert_equal __, ::AboutConstants::C
  end
4

0 回答 0