Ruby:DSL 方法的可见性?
我正在尝试为 Ruby 程序编写 DSL。目前它的工作原理如下:
generator.format
title "Text"
author "John Doe"
body "Text"
end
generator.generate!
我应该为 generator.format
内部调用的方法提供什么可见性?目前,我将每个格式化方法都视为受保护的,但是当我测试它们时,我必须将每个调用包装在 instance_eval
中才能访问它们。我应该声明什么可见性 DSL 方法?
I am trying to write a DSL for a Ruby program. It currently works like this
generator.format
title "Text"
author "John Doe"
body "Text"
end
generator.generate!
What visibility should I give the methods that are called inside of generator.format
? Currently I am treating each of the formatting methods as protected
but when I test them i have to wrap each call in an instance_eval
to access them. What visibility should I declare my DSL methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说好像有一个替代解决方案。
如果您想在没有显式对象的情况下调用方法,则必须使用
instance_eval
更改this
。现在,对于这些方法的可见性,我建议使用尽可能低的可见性,即
private
。You say that like there is an alternative solution.
If you want to invoke your methods without an explicit object, you must change
this
usinginstance_eval
.Now for the visibility of those methods, I suggest using the lowest visibility possible which should be
private
.