【发布时间】:2010-06-01 15:16:08
【问题描述】:
我刚开始在我的 .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 中方法的一般命名约定是小写的。但是括号现在真的在扼杀我的 DSL 语法。
有没有办法解决这个问题?
【问题讨论】: