- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我刚开始在我的 .NET 应用程序中为 DSL 使用 IronRuby(但当我在纯 Ruby 中测试它时,行为似乎是一致的)——作为其中的一部分,我定义了通过 define_method 从 DSL 调用的方法.
但是,在调用以大写字母开头的方法时,我遇到了一个关于可选括号的问题。
给定以下程序:
class DemoClass
define_method :test do puts "output from test" end
define_method :Test do puts "output from Test" end
def run
puts "Calling 'test'"
test()
puts "Calling 'test'"
test
puts "Calling 'Test()'"
Test()
puts "Calling 'Test'"
Test
end
end
demo = DemoClass.new
demo.run
在控制台中运行此代码(使用纯 ruby)会产生以下输出:
ruby .\test.rb
Calling 'test'
output from test
Calling 'test'
output from test
Calling 'Test()'
output from Test
Calling 'Test'
./test.rb:13:in `run': uninitialized constant DemoClass::Test (NameError)
from ./test.rb:19:in `<main>'
我意识到 Ruby 约定是常量以大写字母开头,而 Ruby 中方法的一般命名约定是小写字母。但是现在,parens 真的扼杀了我的 DSL 语法。
有什么办法可以解决这个问题吗?
最佳答案
这只是 Ruby 歧义解决的一部分。
在 Ruby 中,方法和变量位于不同的命名空间中,因此可以有同名的方法和变量(或常量)。这意味着,当使用它们时,需要有某种方式来区分它们。通常,这不是问题:消息有接收者,而变量没有。消息有参数,变量没有。变量被分配给,消息没有。
唯一的问题是当你没有接收者、没有参数也没有赋值时。然后,Ruby 无法区分不带参数的无接收者消息发送和变量之间的区别。因此,它必须制定一些任意规则,这些规则基本上是:
请注意,对于带有参数的消息发送(即使参数列表为空),没有歧义,这就是您的第三个示例有效的原因。
test()
:明明是消息发送,这里没有歧义test
:可能是消息发送,也可能是变量;解析规则说是消息发送Test()
:明明是消息发送,这里没有歧义self.Test
:也是显然是一个消息发送,这里没有歧义Test
:可能是消息发送或常量;解析规则说它是一个常数请注意,这些规则有点微妙,例如:
if false
foo = 'This will never get executed'
end
foo # still this will get interpreted as a variable
规则说,一个不明确的标记是被解释为变量还是消息发送是由解析器决定的,而不是由解释器决定的。所以,因为解析器已经看到了 foo = whatever
,所以它将 foo
标记为一个变量,即使代码永远不会被执行而 foo
会评估为 nil
,就像 Ruby 中所有未初始化的变量一样。
TL;DR 总结:你是 SOL。
您可以做的是覆盖 const_missing
以转换为消息发送。像这样:
class DemoClass
def test; puts "output from test" end
def Test; puts "output from Test" end
def run
puts "Calling 'test'"
test()
puts "Calling 'test'"
test
puts "Calling 'Test()'"
Test()
puts "Calling 'Test'"
Test
end
def self.const_missing(const)
send const.downcase
end
end
demo = DemoClass.new
demo.run
除了这显然行不通,因为 const_missing
是在 DemoClass
上定义的,因此,当 const_missing
运行时,self
是 DemoClass
这意味着它在应该通过 demo 调用
.DemoClass#test
时尝试调用 DemoClass.test
.test
我不知道如何轻松解决这个问题。
关于ruby - Ruby 中带有大写首字母的方法的可选括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2950956/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!