- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
为了方便用户和更干净的代码,我想编写一个可以像这样使用的类:
Encoder::Theora.encode do
infile = "path/to/infile"
outfile = "path/to/outfile"
passes = 2
# ... more params
end
现在的挑战是,让这些参数在我的编码方法中可用。
module Encoder
class Theora
def self.encode(&proc)
proc.call
# do some fancy encoding stuff here
# using the parameters from the proc
end
end
end
这种方法行不通。调用 Proc 时,不会在 Theora 类的上下文中评估变量。通常我想使用 method_missing 将每个参数放入类 Theora 的类变量中,但我没有找到正确的入口方式。
谁能指出我正确的方向?
最佳答案
我不确定是否可以让 DSL 使用赋值,我认为 Ruby 解释器将始终假定 infile
in infile = 'path/to/something'
是该上下文中的局部变量(但可以使 self.infile = 'path/to/something'
起作用)。但是,如果您可以在没有这些特定细节的情况下生活,您可以像这样实现您的 DSL:
module Encoder
class Theora
def self.encode(&block)
instance = new
instance.instance_eval(&block)
instance
end
def infile(path=nil)
@infile = path if path
@infile
end
end
end
并像这样使用它:
Encoder::Theora.encode do
infile 'path/somewhere'
end
(类似地实现其他属性)。
关于ruby - 在 Ruby 的 DSL 中使用 procs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756647/
我们有一个系统,其行为由许多 XML 文件定义。(大约 50 个不同的 XML 文件,每个文件管理子系统的行为。) 出于遗留原因,XML 文件采用自定义格式,旨在方便系统中的各种组件使用。 自定义格式
我发送的 json 请求是: Given url applicationURL And path 'applications' And header Authorization = subscribe
默认情况下,生成的 XText 工件会从我的 DSL 生成代码到默认 socket (默认为 src-gen 文件夹)。我知道您可以在 fsa.generateFile("myfile.txt", "
我的论文主题一般是关于领域特定语言的,我想专注于外部或内部 DSL 的设计或实现,但我什至无法思考或开始,因为我在理解 DSL 的概念方面遇到了问题.. 我已经阅读并收集了很多关于这个问题的论文和调查
我有一个 Xtext 项目和几个示例 DSL 文件。我可以使用“示例 Ecore 模型编辑器”打开这些文件并验证它是否符合 Xtext 生成的元模型。但是,由于 DSL 文件未被识别为 Ecore,我
当我学习一些 DSL 时,我意识到 Rebol 中的 Parse 方言可以是一个很好的词法分析器和解析器。 the Parse tutorial 有一个很好的例子: expr: [ter
我正在考虑使用亚马逊云服务(EC2、S3 等)进行托管。我一直在查看可以指定用于配置各种实例的 JSON 元数据,我担心它的复杂性。是否有一个 dsl 可以生成有效的 JSON 元数据并且更重要的是验
我可能会因为这个而被否决,但无论如何我都会试试运气。 我真的找不到 Elastic Search 查询 DSL 的完整形式“DSL”的任何链接,甚至在 Elastic Search 网站上也找不到 h
这个问题可能是复合的,让我扩展一下: 是否存在设计器( stub /框架/元设计器)来创建基于 .NET 对象公共(public) bool 属性的 AND/OR 规则?保存为任何 DSL/Boo/.
与 anko 一样你可以这样写回调函数: alert { title = "" message = "" yesButton { toast("Yes")
我有一个像下面这样的原始聚合脚本,但是很难将其转换为elasticsearch dsl。 我已阅读该文档并找到描述,说我们可以使用.bucket()、. metric()和.pipeline()方法来
如何将这个 gradle groovy 片段转换为 gradle kotlin dsl 而不是非常冗长? test { systemProperties System.getPropertie
这个问题是 ANY operator with jOOQ 的衍生问题和 Are arrays optimized in jOOQ & PostgreSQL? . 我有一个Field field和Lis
我创建了内部 DSL,并且会重载 DefaultGroovyMethods 中的 any() 方法。 class RulesProcessor { } Any live cell with fewer
我正在尝试使用 Gradle 使用 Kotlin 和 Java 11 构建一个简单的 JavaFX 11 程序,按照说明 here .但是,此页面使用 Gradle 的 Groovy DSL,而我正在
如何将这个 gradle groovy 片段转换为 gradle kotlin dsl 而不是非常冗长? test { systemProperties System.getPropertie
我是 Camel 的新手,我仍在学习它,根据我的理解,你可以在 Spring DSL 中做与 Java DSL 相同的事情。我想知道如何将一种转换为另一种?我经常看到 Java 中的例子,但想要 最佳
我正在尝试通过 Groovy 代码为 Jenkins Job DSL 插件创建 ListView 。运行后, View 已创建,但不会添加任何作业。以下代码段之前的代码创建了构建和部署作业,并且在调用
对于 Jenkins 作业 DSL,我试图为作业选择特定的 ssh 代理(插件)键(在包装器上下文中使用 sshAgent 关键字)。我们安装了 Jenkins ssh 代理插件并设置了几个 key
我必须为非程序员(我们公司的客户)创建一个 DSL,它需要提供一些更高级别的语言功能(循环、条件表达式、变量...... - 所以它不仅仅是一个“简单”的 DSL)。 使用 DSL 应该很容易;人们应
我是一名优秀的程序员,十分优秀!