B
Brian Candler
Hello,
I have some object classes to which I want to add an interactive
command-line shell for testing. At the moment, at the end of the source file
I have something like
if __FILE__ == $0
obj = MyClass.new(*ARGV)
CLI.run(obj, "MyClass> ")
end
Now, I had started writing a CLI module which reads lines from stdin, splits
them into an array, runs obj.send(*args), and prints the response. Then it
occurred to me that this is what IRB is for. But I don't know how to start
IRB in such a way that the default "self" receiver is an object which I
created.
Here's an example of what I'd like to end up with:
class Foo
attr_reader :addr
def initialize(addr)
@addr = addr
end
def do_stuff(n)
n.times { puts "doing stuff with #{@addr}" }
end
end
if __FILE__ == $0
obj = Foo.new(*ARGV)
prompt = "Foo(#{obj.addr})> "
IRB.run(obj, prompt) # <<<--- what do I put here?
end
$ ruby foo.rb 127.0.0.1
Foo(127.0.0.1)> do_stuff 3
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
nil
Foo(127.0.0.1)>
Reading the irb manpage, I can see that you can use the 'irb' command
(interactively) to set the default receiver in a subshell:
irb(main):012:0> obj = Foo.new("127.0.0.1")
=> #<Foo:0xb7c46730 @addr="127.0.0.1">
irb(main):013:0> irb obj
irb#1(#<Foo:0xb7c46730>):001:0> do_stuff 3
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
=> 3
But I can't work out how to invoke an initial IRB instance with my own
'main' object from the start. I've tried going through the source, but I get
lost in Contexts and Workspaces well before I get to subirb.rb I'd also
prefer to implement it in a way which uses a 'standard' interface to Irb,
such that it won't break with a future version of Ruby.
Any clues gratefully received.
Thanks,
Brian.
I have some object classes to which I want to add an interactive
command-line shell for testing. At the moment, at the end of the source file
I have something like
if __FILE__ == $0
obj = MyClass.new(*ARGV)
CLI.run(obj, "MyClass> ")
end
Now, I had started writing a CLI module which reads lines from stdin, splits
them into an array, runs obj.send(*args), and prints the response. Then it
occurred to me that this is what IRB is for. But I don't know how to start
IRB in such a way that the default "self" receiver is an object which I
created.
Here's an example of what I'd like to end up with:
class Foo
attr_reader :addr
def initialize(addr)
@addr = addr
end
def do_stuff(n)
n.times { puts "doing stuff with #{@addr}" }
end
end
if __FILE__ == $0
obj = Foo.new(*ARGV)
prompt = "Foo(#{obj.addr})> "
IRB.run(obj, prompt) # <<<--- what do I put here?
end
$ ruby foo.rb 127.0.0.1
Foo(127.0.0.1)> do_stuff 3
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
nil
Foo(127.0.0.1)>
Reading the irb manpage, I can see that you can use the 'irb' command
(interactively) to set the default receiver in a subshell:
irb(main):012:0> obj = Foo.new("127.0.0.1")
=> #<Foo:0xb7c46730 @addr="127.0.0.1">
irb(main):013:0> irb obj
irb#1(#<Foo:0xb7c46730>):001:0> do_stuff 3
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
doing stuff with 127.0.0.1
=> 3
But I can't work out how to invoke an initial IRB instance with my own
'main' object from the start. I've tried going through the source, but I get
lost in Contexts and Workspaces well before I get to subirb.rb I'd also
prefer to implement it in a way which uses a 'standard' interface to Irb,
such that it won't break with a future version of Ruby.
Any clues gratefully received.
Thanks,
Brian.