- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有机会在 StackOverflow 中四处看看,发现了我试图从 Ruby Koans (Ruby Koans: explicit scoping on a class definition part 2) 中更好地理解的同一个问题。
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?
根据链接中的解释,其他人(包括我自己)的主要困惑似乎是因为类定义:
class MyAnimals::Oyster < Animal
# stuff goes in here
end
我最初的想法是 MyAnimals::Oyster 意味着 Oyster 类是在 MyAnimals 中定义的。换句话说,我认为上面的代码类似于下面的代码:
class MyAnimals
class Oyster < Animal
# stuff goes in here
end
end
为了验证我的想法,我在 IRB 中做了以下操作:
class MyAnimals
LEGS = 2
class Bird < Animal
def legs_in_bird
LEGS
end
end
end
class MyAnimals::Oyster # You should notice that I'm not inheriting from Animal anymore
def legs_in_oyster
LEGS
end
end
如果我的推理是正确的,那么我希望下面的代码返回 2
MyAnimals::Oyster.new.legs_in_oyster # => NameError: uninitialized constant MyAnimals::Oyster::LEGS
因为这不返回 2,有人可以向我解释为什么它不返回 2 吗?
编辑:我忘了添加 Animal 类;这是:
class Animal
LEGS = 4
def legs_in_animal
LEGS
end
class NestedAnimal
def legs_in_nested_animal
LEGS
end
end
end
最佳答案
回到 Ruby 搜索值的顺序。第 1 步是“封闭范围”。当你像这样定义一个嵌套类时
class Animal
class Bird
end
end
'Bird' 包含在 'Animal' 范围内。
当你这样做的时候
class Animal
end
class Animal::Bird
end
虽然您已将“Bird”定义为嵌套在“Animal”中,但在您定义“Bird”时,封闭范围是global 范围。返回 IRB 并定义 LEGS = 0
(在全局范围内)并再次尝试您的 Oyster。
关于Ruby Koans - 词法范围的延续与继承层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15101544/
我试图了解传递给 setTimeout 的箭头函数如何记住上一个执行上下文中的 this 的值。我知道在执行箭头函数时会使用词法作用域规则查找 this 值。这是否意味着箭头函数关闭变量和 this
这个问题已经有答案了: How does the "this" keyword in Javascript act within an object literal? [duplicate] (4 个
我已阅读 this问题,我想我已经理解了投票最多的答案,但他说 since basically every programming language in wide use today uses le
如何让这段宏发挥预期的作用? -- 我想从词法环境中捕获 p 而不必将其作为参数发送给宏。 (define-syntax-rule (fi a b) (if p a b)) ;--->capt
Program A() { x, y, z: integer; procedure B() { y: integer; y=0;
我正在用 Java 实现自己的链表。节点类只有一个名为“name”的字符串字段和一个名为“link”的节点。现在我有一个测试驱动程序类,它只按顺序插入几个名字。现在,我正在尝试编写一种排序方法来按字母
考虑到这个question SO,其中调用了整个 C# 内存中编译器。只有lexical and syntactic analyzing时是必需的:将文本解析为词素流,检查它们并退出。 在System
我有 2 个场景。 这失败了: class F { public X X { get; set; } } 错误 CS0102:类型“F” ' 已经包含 ' X 的定义| ' 这个有效: class
我有一个用 NodeJS 执行的 .js 文件。这是我的文件的内容: var ctry = "America"; function outer(msg) { console.log(msg +
我对编写汇编程序的概念非常陌生,即使在阅读了大量 Material 之后,我仍然很难理解几个概念。 将源文件实际分解为 token 的过程是什么?我相信这个过程称为词法分析,我已经到处搜索有意义的真实
在 static scoping,标识符可以通过分析/解析源代码来确定(与动态作用域不同,动态作用域或多或少需要了解调用者环境)。 我的问题是这样的,因为静态作用域只需要解析源代码以了解作用域和标识符
编辑:我在第一个答案后更改了示例代码,因为我想出了一个简单的版本来回避相同的问题。 我目前正在学习 Common Lisp 的作用域属性。在我认为我有一个坚实的理解之后,我决定编写一些我可以预测结果的
考虑这段代码: class Bar(object): pass class Foo(object): def bar(self): return Bar() f = Foo() def Bar
将 ES6 箭头函数与词法 this 绑定(bind)结合使用非常棒。 但是,我刚才在使用典型的 jQuery 单击绑定(bind)时遇到了一个问题: class Game { foo() {
将 ES6 箭头函数与词法 this 绑定(bind)结合使用非常好。 但是,我刚才在将它与典型的 jQuery 点击绑定(bind)一起使用时遇到了一个问题: class Game { foo(
我是一名优秀的程序员,十分优秀!