开发者

Re-using Ruby DSL in a REPL or irb?

开发者 https://www.devze.com 2023-03-12 09:13 出处:网络
I have developed a simple DSL for tasks on a UniVerse database in jruby. It looks something like this

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 
    conn开发者_如何学Pythonect
    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


In irb you can use the irb command to move inside an object scope:

irb> irb some_object

from then on any commands will execute inside the scope of that object (so you can call its instance methods directly).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号