Ruby Koans:类定义的显式范围第 2 部分 [英] Ruby Koans: explicit scoping on a class definition part 2

查看:30
本文介绍了Ruby Koans:类定义的显式范围第 2 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清这篇原始帖子的一些内容.答案建议 Ruby 按以下顺序搜索常量定义:

I wanted to clarify some things off of this original post. The answer suggested that Ruby searches for the constant definition in this order:

  1. 封闭范围
  2. 任何外部作用域(重复直到达到顶级)
  3. 包含的模块
  4. 超类
  5. 对象
  6. 内核

那么澄清一下,在第 (1-6) 步中为 legs_in_oyster 找到的常量 LEGS 的值是什么?是否来自超类Animal?类 MyAnimals 的范围是否被忽略,因为它不被视为封闭范围?这是由于显式的 MyAnimals::Oyster 类定义造成的吗?

So to clarify, at which step (1-6) is the value for the constant LEGS found for legs_in_oyster? Is it from the Superclass Animal? Is the scope of class MyAnimals ignored because it is not considered an enclosing scope? Is this due to the explicit MyAnimals::Oyster class definition?

谢谢!只是试图理解.代码如下:

Thanks! Just trying to understand. Here is the code:

 class Animal
   LEGS = 4
   def legs_in_animal
     LEGS
   end

   class NestedAnimal
     def legs_in_nested_animal
       LEGS
     end
   end
 end

 def test_nested_classes_inherit_constants_from_enclosing_classes
   assert_equal 4, Animal::NestedAnimal.new.legs_in_nested_animal
 end

 # ------------------------------------------------------------------

 class MyAnimals
   LEGS = 2

   class Bird < Animal
     def legs_in_bird
       LEGS
     end
   end
 end

 def test_who_wins_with_both_nested_and_inherited_constants
   assert_equal 2, MyAnimals::Bird.new.legs_in_bird
 end

 # QUESTION: Which has precedence: The constant in the lexical scope,
 # or the constant from the inheritance heirarachy?

 # ------------------------------------------------------------------

 class MyAnimals::Oyster < Animal
   def legs_in_oyster
     LEGS
   end
 end

 def test_who_wins_with_explicit_scoping_on_class_definition
   assert_equal 4, MyAnimals::Oyster.new.legs_in_oyster
 end

 # QUESTION: Now Which has precedence: The constant in the lexical
 # scope, or the constant from the inheritance heirarachy?  Why is it
 # different than the previous answer?
 end

推荐答案

我只是在思考同一个公案中的同一个问题.我不是范围界定方面的专家,但以下简单的解释对我来说很有意义,也许对您也有帮助.

I was just pondering the very same question from the very same koan. I am no expert at scoping, but the following simple explanation made a lot of sense to me, and maybe it will help you as well.

当您定义 MyAnimals::Oyster 时,您仍然处于全局范围内,因此 ruby​​ 不知道 MyAnimals<中设置为 2 的 LEGS 值/code> 因为你实际上从未在 MyAnimals 的范围内(有点违反直觉).

When you define MyAnimals::Oyster you are still in the global scope, so ruby has no knowledge of the LEGS value set to 2 in MyAnimals because you never actually are in the scope of MyAnimals (a little counterintuitive).

但是,如果您以这种方式定义 Oyster,情况会有所不同:

However, things would be different if you were to define Oyster this way:

class MyAnimals
  class Oyster < Animal
    def legs_in_oyster
      LEGS # => 2
    end
  end
end

不同的是,在上面的代码中,当你定义Oyster时,你已经落入了MyAnimals的范围内,所以ruby知道LEGS 指的是 MyAnimals::LEGS (2) 而不是 Animal::LEGS (4).

The difference is that in the code above, by the time you define Oyster, you have dropped into the scope of MyAnimals, so ruby knows that LEGS refers to MyAnimals::LEGS (2) and not Animal::LEGS (4).

仅供参考,我从以下网址(在您链接到的问题中引用)获得了这一见解:

FYI, I got this insight from the following URL (referenced in the question you linked to):

这篇关于Ruby Koans:类定义的显式范围第 2 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆