Ruby Koans about_constants.rb

时间:2013-10-15 03:51:56

标签: ruby constants

完成Hartl's 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

0 个答案:

没有答案