在 REPL 或 irb 中重新使用 Ruby DSL?
我为 jruby 中 UniVerse 数据库上的任务开发了一个简单的 DSL。它看起来像这样,
support = {
:host => 'localhost',
:account => 'SUPPORT'
}
uni_task support do
connect
exec "LIST FILE A1"
disconnect
end
并且实现方式如下
def uni_task(config, &block)
session = UniSession.new
session.instance_eval &block
end
我知道您可以在 ruby 脚本中将其拖放到 irb,例如 this
但是有没有办法下降到命令行并将范围更改为默认执行对象的实例方法?
例如
irb> uni_commandline support
uni> connect
uni> exec "LIST FILE A1"
.... output .....
uni> disconnect
I have developed a simple DSL for tasks on a UniVerse database in jruby. It looks something like this
support = {
:host => 'localhost',
:account => 'SUPPORT'
}
uni_task support do
connect
exec "LIST FILE A1"
disconnect
end
and is implemented like this
def uni_task(config, &block)
session = UniSession.new
session.instance_eval &block
end
I'm aware that you can drop to irb in a ruby script like this
But is there a way to drop to a command line and have the scope changed to execute instance methods of an object by default?
Eg
irb> uni_commandline support
uni> connect
uni> exec "LIST FILE A1"
.... output .....
uni> disconnect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 irb 中,您可以使用 irb 命令在对象范围内移动:
从那时起,任何命令都将在该对象的范围内执行(因此您可以直接调用其实例方法)。
In irb you can use the
irb
command to move inside an object scope:from then on any commands will execute inside the scope of that object (so you can call its instance methods directly).