初学 ruby, 在学习 ruby_koans,遇到以下问题:
class ::Integer
def even?
(self % 2) == 0
end
end
def test_even_existing_built_in_classes_can_be_reopened
assert_equal __, 1.even?
assert_equal __, 2.even?
end
# NOTE: To understand why we need the :: before Integer, you need to
# become enlightened about scope.
但是去掉 ::
也没有区别啊
class Integer
def even?
(self % 2) == 0
end
end
def test_even_existing_built_in_classes_can_be_reopened
assert_equal __, 1.even?
assert_equal __, 2.even?
end
# NOTE: To understand why we need the :: before Integer, you need to
# become enlightened about scope.