Re-using Ruby DSL in a REPL or irb?
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).
精彩评论